从MongoDb中的数组数组中删除元素

时间:2015-12-29 08:57:08

标签: arrays mongodb mongoose mongodb-query

以下是阵列联系人的架构。 contacts数组有一个字段hashtag,它是另一个数组。如何从数组Hashtags中删除元素openLove?

"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

2 个答案:

答案 0 :(得分:1)

您通常会使用 positional operator $ 执行此操作,如下所示:

db.collection.update(
    { "contacts.hashtag": "openLove" },
    {
        "$pull": {
            "contacts.$.hashtag": "openLove"
        }
    }
)

但是,因为它只支持一级深度数组(位置 $ operator 充当与查询文档匹配的第一个元素的占位符),所以只匹配第一个元素查询文档已删除。这里有一个JIRA可跟踪:https://jira.mongodb.org/browse/SERVER-831

如果你知道预先删除了elememt的hashtags数组的索引,那么更新查询将是:

db.collection.update(
    { "contacts.hashtag": "openLove" },
    {
        "$pull": {
            "contacts.0.hashtag": "openLove",
            "contacts.1.hashtag": "openLove"
        }
    }
)

考虑重新设计架构以避免嵌套数组,以便您可以通过创建单独的联系人集合来规范化集合,其中每个文档代表一个联系人,其中包含一组在原始集合中重复的联系人的共同信息。如下所示:

收集架构:

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

更新联系人集合会更容易,只需运行操作

即可
db.contacts.update(
    { "hashtag": "openLove" },
    {
        "$pull": { "hashtag": "openLove" }
    }
)

如果重新设计架构超出了您的范围,那么您需要一种机制来动态生成更新文档,即动态创建 $pull 对象。考虑使用 Map-Reduce 生成, answer 详细说明整个操作概念。

答案 1 :(得分:0)

chridam's回答的帮助下,我通过使用每个联系人的对象ID来使其工作。

db.user.update(
{ "contacts._id": ObjectId("5680f392e623e8b2107e6465") },
{
    "$pull": {
        "contacts.$.hashtag": "openLove"
    }
})