从 MongoDb 中数组的数组中删除一个元素 [英] Delete an element from an array of an array in MongoDb
问题描述
以下是数组联系人的架构.联系人数组有一个字段 hashtag,它是另一个数组.如何从数组 Hashtags 中删除元素 openLove?
Below is the schema for an array contacts. The contacts array has a field hashtag which is another array. How do I delete an element openLove from the array Hashtags?
"contacts" : [
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "tell.fadgfdg@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.fadgfdg@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.fadgfdg@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
"verified" : true,
"favorite" : true,
"linkedinUserName" : null,
"facebookUserName" : null,
"twitterUserName" : "IamlifePaul",
"count" : 2,
"relationshipStrength_updated" : 0,
"contactRelation" : {
"decisionmaker_influencer" : null,
"prospect_customer" : "prospect"
},
"source" : "abc",
"mobileNumber" : "3546789",
"skypeId" : "123",
"designation" : "test",
"companyName" : "Something",
"location" : "Hyderabad, Telangana, India",
"personName" : "Naveen Paul",
"personId" : "565022d7dbeaeb9e17fc7083",
"hashtag" : [
"polly",
"tagger",
"#hash",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
}
如何从Hashtags数组中删除一个元素?例如删除 openLove?
How do I delete an element from the array Hashtags? for example remove openLove?
推荐答案
您通常会使用 位置运算符$
如下:
db.collection.update(
{ "contacts.hashtag": "openLove" },
{
"$pull": {
"contacts.$.hashtag": "openLove"
}
}
)
然而,由于这仅支持一级深度数组(位置$ operator
充当匹配查询文档的第一个元素的占位符),仅匹配查询文档的第一个元素已移除.这里有一个 JIRA 可跟踪:https://jira.mongodb.org/browse/SERVER-831
However, as this only supports one-level deep arrays (the positional $ operator
acts as a placeholder for the first element that matches the query document), only the first element that matches the query document is removed. There is a JIRA trackable for this here: https://jira.mongodb.org/browse/SERVER-831
如果您事先知道要删除元素的 hashtags 数组的索引,则更新查询将是:
If you know the index of the hashtags array that has the elememt to be removed beforehand then the update query will be:
db.collection.update(
{ "contacts.hashtag": "openLove" },
{
"$pull": {
"contacts.0.hashtag": "openLove",
"contacts.1.hashtag": "openLove"
}
}
)
考虑重新设计您的架构以避免嵌套数组,以便您可以通过创建一个单独的联系人集合来规范您的集合,其中每个文档代表一个联系人,信息与原始集合中重复的一组联系人相同.类似于以下内容:
Consider redesigning your schema to avoid nested arrays so that you can normalize your collection by creating a separate contacts collection where each document represents a contact, with information common to a set of contacts duplicated in the original collection. Something like the following:
集合架构:
{
_id: collection_id,
contacts: [
ObjectId("565eb481bf35eeb83d7f9f13"),
ObjectId("565eb481bf35eeb83d7f9f14"),
ObjectId("565eb481bf35eeb83d7f9f15")
]
}
联系人架构:
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "tell.fadgfdg@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
...
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.fadgfdg@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f14"),
...
"hashtag" : [
"latestTag",
"anotherTag",
"#hash",
"openLove",
"hellTwo",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
},
{
"addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
"personEmailId" : "naveenpaul.eewsdf@gmail.com",
"_id" : ObjectId("565eb481bf35eeb83d7f9f15"),
...
"hashtag" : [
"polly",
"tagger",
"#hash",
"working?",
"hello",
"lol",
"zxc"
],
"lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
}
更新联系人集合会更容易,只需运行操作
Updating the contacts collection would be easier, simply run the operation
db.contacts.update(
{ "hashtag": "openLove" },
{
"$pull": { "hashtag": "openLove" }
}
)
如果重新设计架构超出您的范围,那么您需要一种机制来动态生成更新文档,即创建 $pull
对象.考虑使用 Map-Reduce 来生成它,这个答案详细说明了整个操作概念.
If redesigning the schema is out of your scope then you would need a mechanism to dynamically generate the update document i.e. create the $pull
object on the fly. Consider using Map-Reduce to generate that, this answer details the whole operation concept.
这篇关于从 MongoDb 中数组的数组中删除一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!