2012-04-20 472 views
2

我有一個架構(稱爲事件)的數據,看起來像這樣:貓鼬/ MongoDB的 - 刪除嵌入文檔

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

我怎樣才能「刪除」與會者ID爲4f8dfb06ee21783d7134503c對象?

基本上類似....

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

你知道,發佈有效的代碼可以提供幫助。 – 2012-04-20 12:57:28

+0

哪位不合法? – Alex 2012-04-20 13:24:47

+1

嘗試插入帖子中的對象。 – 2012-04-20 13:26:48

回答

8

我認爲你正在尋找$pull

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