Mongoose / mongodb - 删除嵌入的文档

时间:2012-04-20 12:11:14

标签: mongodb mongoose

我有一个Schema(称为Event),其数据如下所示:

{ "_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
});

1 个答案:

答案 0 :(得分:9)

我认为你正在寻找$pull

db.collection.update({_id: ObjectId( "4f8dcb06ee21783d7400003c" )}, 
                     {$pull: {attendees: {_id: ObjectId( "4f8dfb06ee21783d7134503a" )}}})