本文介绍了流星mongo驱动程序可以处理$ each和$ position运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用流星束最新的流星应用程序.我想在mongo文档数组中重新放置项目.为此,我将其$pull从数组中移出,然后根据 mongoDB文档.

I work on a meteor application with meteor bundle up to date. I want to reposition an item within a mongo document array. To achieve that, I $pull it out of the array, and then $push it at a specific index position according to mongoDB documentation.

MyCollection.update({_id: my_doc.id},
        {
          $push:
          {
            my_array:
            {
              $each:[my_item.id],
              $position:index
            }
          }
        }
      )

流星/蒙哥抛出以下错误:

Meteor/Mongo throws the following error:

我首先实现了此客户端.我认为由于minimongo的限制,它无法正常工作.

I first implemented this client side. I assumed it didn't work because of minimongo limitations.

我编写了一种处理此服务器端的方法,但最终还是遇到相同的错误.

I wrote a method to handle this server side, but I end up with the same error.

此请求有什么问题,流星可以处理$ each运算符吗?

What is wrong with this request, can meteor handle the $each operator?

EDIT :我试图将其直接插入到robomongo中,并且可以正常工作. Mongo版本,在键入db.version()时返回2.6.7

EDIT : I tried to insert it directly in robomongo, and it worked. Mongo version, when typing db.version() returns 2.6.7

* 我没想到,所以我之前没有检查过:此更新适用于$pull$push.但是,即使实际更新了数据,我仍然会收到错误消息.

*EDIT2 : I did not expect it so I didn't check before: the update works, both with the $pull and the $push. However, even if the data is actually updated, I still get the error.

* 这是一些示例数据:

*EDIT : Here is some example data:

{
    "_id" : "oSNrpgAAu8BuznvD6",
    "name" : "tynhjderjye",
    "description" : "",
    "notes" : "",
    "display_notes" : false,
    "keywords" : [
        ""
    ],
    "owner" : "mA5Y7LBCoRyeSDkaG",
    "createdAt" : ISODate("2015-10-27T13:59:06.083Z"),
    "createdBy" : "C3i9oj4eapyttHZj6",
    "contributors" : [
        "C3i9oj4eapyttHZj6"
    ],
    "medias" : [
        "TcFqermNY4y5cjBG3",
        "dbkNN2rxXJXth8urw",
        "jML4JKkRoKxx8sLwu",
        "LEWYsnPrXRSH6MPkX"
    ],
    "modifiedAt" : ISODate("2015-11-17T09:35:50.303Z"),
    "modifiedBy" : "C3i9oj4eapyttHZj6",
    "chunks" : [
        "qCCHKJDbdTLEFR5Yt",
        "ySiM7dcxvduEM2npj",
        "5q46vqrmYttscitiK"
    ],
    "trashed" : ISODate("2015-11-17T09:35:50.303Z")
}

chunks是数组my_array,在其中我将my_item.id推到位置index

chunks is the array my_array where I pull and push the my_item.id at the position index

推荐答案

如果使用new Mongo.Collection('col')创建Meteor集合,您将获得一个非本机Node MongoDriver的Minimongo实例,对吗?

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?

因此某些方法不起作用或不完全受支持..如collection.aggregate

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

但是您可以通过Col.rawCollection()轻松访问本机驱动程序,并直接在本机实例上执行查询. 自然实例只能在服务器上访问.

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 }我个人比较喜欢这种方式,因为您只需要执行一次更新操作,而不是两次($pull& $push at $position)

$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)

但是,如果您想在$ position方式下使用$ push进行操作..只需使用本机驱动程序即可

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 */
);

注意:由于Meteor同步样式,您需要Meteor.wrapAsync,也可以不使用它. Collection.rawCollection().update(...)

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

这篇关于流星mongo驱动程序可以处理$ each和$ position运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:03