从MongoDb中的数组的数组中删除元素 [英] Delete an element from an array of an array in MongoDb
问题描述
下面是数组联系人的架构. Contacts数组具有一个字段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?
推荐答案
You would normally do this using the positional operator $
as follows:
db.collection.update(
{ "contacts.hashtag": "openLove" },
{
"$pull": {
"contacts.$.hashtag": "openLove"
}
}
)
但是,由于它仅支持一级深度数组(位置 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
如果您知道预先删除了elememt的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"
}
}
)
请考虑重新设计架构以避免嵌套数组,以便您可以通过创建一个单独的contacts集合(每个文档代表一个联系人)来标准化您的集合,其中原始集合中重复的一组联系人所共有的信息.类似于以下内容:
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" }
}
)
如果重新设计架构超出您的范围,那么您将需要一种机制来动态生成更新文档,即创建 Map-Reduce 来生成,此 answer 详细介绍了整个操作概念.
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屋!