Can meteor mongo driver handle $each and $position operators?

人走茶凉 提交于 2019-12-04 22:45:39

If you create a Meteor Collection with new Mongo.Collection('col') you get back a Minimongo instance which is not the native Node MongoDriver, right?

So some methods just don't work or not fully supported.. like collection.aggregate

But you can easily access the native driver via Col.rawCollection() and perform your query directly on the native instance. The native instance is only accessible on the server, of course.

So to do what you want do you have several ways, for example you could first take the array, resort it how you want and

$set: {my_array: sortedArray } Personally I would prefer this way because you need to do only one update operation instead of two ($pull & $push at $position)

But if you want to do it the $push at $position way.. just do it with the native driver

var col = Collection.rawCollection();
var result = Meteor.wrapAsync(col.update.bind(col)(
  /* update query goes here */
);

Note: You need the Meteor.wrapAsync because of Meteor sync style, you could also do it with out it. Collection.rawCollection().update(...)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!