0

我在我的 mongo-db 中创建一个新对象时遇到问题。我正在使用流星框架,我想将新的子项添加到一个子项数组中。

我的数据结构如下所示:

    { 
       "_id" : "f9d01fd1ef22684353149851", 
       "name" : "Ueberschrift", 
       "items" : [     
                  { "_id" : "be695ec7ffe71152088c57e1",
                    "name" : "asd",
                    "checked" : false,
                    "subitems" : [ ] 
                  },
                  { "_id" : "fe665ec7ffe78852088c22g8",
                    "name" : "asd",
                    "checked" : false,
                    "subitems" : [ ] 
                  } 
                 ] 
     },
     { 
          "_id" : "qw501fd1we522683531qw451",    
          .........
     }, ...

例如,我想添加子项

 { _id : 1, name : "MySubitemName" }

到具有 _id 的项目:"be695ec7ffe71152088c57e1"
在对象中"f9d01fd1ef22684353149851"

我使用以下命令来执行此操作:

uid => "f9d01fd1ef22684353149851" and
id => "be695ec7ffe71152088c57e1"

Listitems.update({_id:uid, "items._id" : id}, 
  {"items.$.subitems":  {$push: { _id : 1, name : "MySubitemName" }}});

但它不起作用,数据库看起来像以前一样。

有人有想法吗?

4

2 回答 2

0

One thing is suggested when defining your doc structure: Avoid using _id in your own schema, as this name is internal of mongodb to represent the ObjectId. If it is strict that you do so, use id instead. Please see http://docs.mongodb.org/manual/reference/object-id/.

So your document can now be like:

{ 
    "id" : "f9d01fd1ef22684353149851", 
     "name" : "Ueberschrift", 
     "items" : [     
            { 
                "id" : "be695ec7ffe71152088c57e1",
                "name" : "iop",
                "checked" : false,
                "subitems" : [ ] 
            },
            { 
                "id" : "fe665ec7ffe78852088c22g8",
                "name" : "asd",
                "checked" : false,
                "subitems" : [ ] 
            } 
         ] 
 }

the update query:

Listitems.update({ id:"f9d01fd1ef22684353149851", "items.id" : "be695ec7ffe71152088c57e1"},  {$push: {"items.$.subitems" :{ id : 1, name : "MySubitemName" }}});

I've tested using mongodb console, and I'm assuming the object supplied in the update query matches that of the console.

Please tell us if that worked.

EDIT to improve comment: Since you are meaning the actual object id (_id) when querying, please have a look a the code below, adapted as a possible fix for the object referencing:

uid => ObjectId("f9d01fd1ef22684353149851") and
id => ObjectId("be695ec7ffe71152088c57e1")

Listitems.update({_id:uid, "items._id" : id}, 
  {$push: {"items.$.subitems" :{ id : 1, name : "MySubitemName" }}});
于 2013-11-14T17:19:30.713 回答
0

问题可能是 minimongo 还不支持 mongo 的“$”占位符运算符。如果是这种情况,您的更新可能在从服务器调用时运行良好,但从客户端调用时会失败。您目前从哪里调用该更新?

有关 Meteor 团队提供的解决方法示例,请在此处查看其“派对”应用程序的 model.js: https ://github.com/meteor/meteor/blob/devel/examples/parties/model.js

我已经粘贴了下面的关键行 144-156,包括他们的评论 - 简而言之,他们在服务器上使用“$”并将其替换为客户端上的数组索引。

if (Meteor.isServer) {
    // update the appropriate rsvp entry with $
    Parties.update(
      {_id: partyId, "rsvps.user": this.userId},
      {$set: {"rsvps.$.rsvp": rsvp}});
  } else {
    // minimongo doesn't yet support $ in modifier. as a temporary
    // workaround, make a modifier that uses an index. this is
    // safe on the client since there's only one thread.
    var modifier = {$set: {}};
    modifier.$set["rsvps." + rsvpIndex + ".rsvp"] = rsvp;
    Parties.update(partyId, modifier);
  }
于 2013-11-14T23:21:46.537 回答