如何从 Mongodb/mongoose 中的对象中删除数组中的元素
How to Delete an element from the array which is present inside an object in Mongodb/mongoose
这是集合文档的结构'conversations'
{
"_id" : ObjectId("553778c0d3adab10206060db"), //conversation id
"messages" : [
{
"from" : ObjectId("5530af38576214dd3553331c"),
"_id" : ObjectId("553778c0d3adab10206060dc"),//message id
"created" : ISODate("2015-04-22T10:32:32.056Z"),
"read" : false,
"message" : "second object first msg",
"participants" : [
ObjectId("5530af38576214dd3553331c"), //participant id
ObjectId("553777f2d3adab10206060d8")//participant id
]
},
{
"from" : ObjectId("5530af38576214dd3553339b"),
"_id" : ObjectId("553778c0d3adab10206060dc"),//message id
"created" : ISODate("2015-04-22T10:32:32.059Z"),
"read" : false,
"message" : "second object second msg",
"participants" : [
ObjectId("5530af38576214dd3553331c"),//participant id
ObjectId("553777f2d3adab10206060d8")//participant id
]
}
],
"participants" : [
ObjectId("5530af38576214dd3553331c"),
ObjectId("553777f2d3adab10206060d8")
],
"__v" : 0
}
每个文档包含 'messages' 数组,该数组又包含作为数组元素的消息对象。每个对象都有参与者数组。
我有对话 ID、消息 ID、参与者 ID。
我想从 'participants' 数组( 'participants' 数组存在于 'messages' 数组的消息对象中)中删除特定元素。
我试过这段代码。
var query = { _id: mongoose.Types.ObjectId(req.conversation.id), 'messages._id':req.params.messageId};
Conversation.findOneAndUpdate(query, {$pull: {'participants' : participant_id}}, function(err, data){})
但它是从外部 'participants' 数组中删除对象元素。
请帮助我完成这项工作。
谢谢
勾选mongopositional operator,查询如下:
db.conversations.update({
"_id": ObjectId("553778c0d3adab10206060db"),
"messages": {
"$elemMatch": {
"_id": ObjectId("553778c0d3adab10206060dc")
}
},
"messages": {
"$elemMatch": {
"participants": {
"$in": [ObjectId("5530af38576214dd3553331c")]
}
}
}
}, {
"$pull": {
"messages.$.participants": {
"$in": [ObjectId("5530af38576214dd3553331c")]
}
}
})
这从匹配的 message
数组中删除给定的 participants
对象。希望这对您有所帮助,您应该将其转换为 mongoose。
这是集合文档的结构'conversations'
{
"_id" : ObjectId("553778c0d3adab10206060db"), //conversation id
"messages" : [
{
"from" : ObjectId("5530af38576214dd3553331c"),
"_id" : ObjectId("553778c0d3adab10206060dc"),//message id
"created" : ISODate("2015-04-22T10:32:32.056Z"),
"read" : false,
"message" : "second object first msg",
"participants" : [
ObjectId("5530af38576214dd3553331c"), //participant id
ObjectId("553777f2d3adab10206060d8")//participant id
]
},
{
"from" : ObjectId("5530af38576214dd3553339b"),
"_id" : ObjectId("553778c0d3adab10206060dc"),//message id
"created" : ISODate("2015-04-22T10:32:32.059Z"),
"read" : false,
"message" : "second object second msg",
"participants" : [
ObjectId("5530af38576214dd3553331c"),//participant id
ObjectId("553777f2d3adab10206060d8")//participant id
]
}
],
"participants" : [
ObjectId("5530af38576214dd3553331c"),
ObjectId("553777f2d3adab10206060d8")
],
"__v" : 0
}
每个文档包含 'messages' 数组,该数组又包含作为数组元素的消息对象。每个对象都有参与者数组。
我有对话 ID、消息 ID、参与者 ID。
我想从 'participants' 数组( 'participants' 数组存在于 'messages' 数组的消息对象中)中删除特定元素。 我试过这段代码。
var query = { _id: mongoose.Types.ObjectId(req.conversation.id), 'messages._id':req.params.messageId};
Conversation.findOneAndUpdate(query, {$pull: {'participants' : participant_id}}, function(err, data){})
但它是从外部 'participants' 数组中删除对象元素。 请帮助我完成这项工作。
谢谢
勾选mongopositional operator,查询如下:
db.conversations.update({
"_id": ObjectId("553778c0d3adab10206060db"),
"messages": {
"$elemMatch": {
"_id": ObjectId("553778c0d3adab10206060dc")
}
},
"messages": {
"$elemMatch": {
"participants": {
"$in": [ObjectId("5530af38576214dd3553331c")]
}
}
}
}, {
"$pull": {
"messages.$.participants": {
"$in": [ObjectId("5530af38576214dd3553331c")]
}
}
})
这从匹配的 message
数组中删除给定的 participants
对象。希望这对您有所帮助,您应该将其转换为 mongoose。