3

我有一个模式(称为事件),其数据如下所示:

{ "_id" : ObjectId( "4f8dcb06ee21783d7400003c" ),
  "venue" : ObjectId( "4f8dcb06ee21783d7400003b" ),
  "name" : "Some event",
  "webite: "www.whatever.com",
  "attendees" : [
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503a" ), "firstName" : "Joe", "lastName" : "Blogs", "emailAddress" : "some@thing1.com" },
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503b" ), "firstName" : "John", "lastName" : "West", "emailAddress" : "some@thing2.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503c" ), "firstName" : "Simon", "lastName" : "Green", "emailAddress" : "some@thing3.com" }
                    { "_id" : ObjectId( "4f8dfb06ee21783d7134503d" ), "firstName" : "Harry", "lastName" : "Smith", "emailAddress" : "some@thing4.com" }
                ],
  "eventType" : "Party"
}

如何“删除” ID 为 4f8dfb06ee21783d7134503c 的参加者对象?

基本上像......

Event.findOne('attendees._id' : ObjectId('4f8dfb06ee21783d7134503c'), function(err, eventItem){
    //delete the attendee embedded doc with id 4f8dfb06ee21783d7134503c
});
4

1 回答 1

10

我认为您正在寻找$pull

db.collection.update({_id: ObjectId( "4f8dcb06ee21783d7400003c" )}, 
                     {$pull: {attendees: {_id: ObjectId( "4f8dfb06ee21783d7134503a" )}}})
于 2012-04-20T13:00:38.150 回答