gpt4 book ai didi

mongodb - Mongoose/mongodb - 删除嵌入文档

转载 作者:可可西里 更新时间:2023-11-01 09:20:32 26 4
gpt4 key购买 nike

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

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

最佳答案

我认为您正在寻找 $pull .

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

关于mongodb - Mongoose/mongodb - 删除嵌入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10246004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com