在列表中添加项目

时间:2013-01-25 11:44:19

标签: mongodb

见下面的代码:

Collection:
 [
  {
    _id: 'abc',
    UserName: 'aaaa',
    Password: 'xxxx',
    Friends: 
          [
               {
                  FriendId: 'bbb',
                  Confirm: 1
               },
               {
                  FriendId: 'ccc'
                  Confirm: 0
               }
          ]
  },
  {
    _id: 'cdk',
    UserName: 'bbbbbbbb',
    Password: 'xxxxzz',
    Friends: 
          [
               {
                  FriendId: 'bbb',
                  Confirm: 1
               },
               {
                  FriendId: 'aaa'
                  Confirm: 0
               }
          ]
    }
  ]

我想在{ Friend: 'ddd', Confirm: 0 }中添加item _id: 'abc'

1 个答案:

答案 0 :(得分:0)

您需要$push - 运营商:

db.collection.update( { "_id": "abc" }, { $push: { Friends: { Friend: 'ddd, Confirm: 0 } } } );