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

标签 arrays mongodb mongoose mongodb-query

下面是数组联系人的架构。联系人数组有一个字段标签,它是另一个数组。如何从数组 Hashtags 中删除元素 openLove?

"contacts" : [
    {
        "addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
        "personEmailId" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadecfc6c684cccbcecdcccecdeacdc7cbc3c684c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>",
        "_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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1bfb0a7b4b4bfa1b0a4bdffb7b0b5b6b7b5b691b6bcb0b8bdffb2bebc" rel="noreferrer noopener nofollow">[email protected]</a>",
        "_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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="761817001313180617031a581017121110121136111b171f1a5815191b" rel="noreferrer noopener nofollow">[email protected]</a>",
        "_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")
    }

如何从数组标签中删除元素? 例如删除openLove

最佳答案

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

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

但是,由于这仅支持单级深度数组(位置 $ operator 充当与查询文档匹配的第一个元素的占位符),因此仅支持与查询匹配的第一个元素文档被删除。这里有一个可追踪的 JIRA:https://jira.mongodb.org/browse/SERVER-831

如果您事先知道包含要删除的元素的主题标签数组的索引,则更新查询将为:

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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8b9a9393d1999e9b98999b98bf98929e9693d19c9092" rel="noreferrer noopener nofollow">[email protected]</a>",
    "_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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed838c9b8888839d8c9881c38b8c898a8b898aad8a808c8481c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>",
    "_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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5bbb4a3b0b0bba5b4a0b9fbb0b0a2a6b1b395b2b8b4bcb9fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>",
    "_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 详细介绍了整个运营理念。

关于arrays - 从 MongoDb 中的数组的数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34507696/

相关文章:

c - 在此代码中使用 realloc - 我的数组大小没有增加

mongodb - 在 Master - Master Configuration - 的情况下,最终一致性是否可能 -

javascript - 将 mongo 数据库连接导出到 models.js 文件

MongoDB:搜索查询以返回数组内的对象

node.js - forEach 在 Node.js 中使用生成器

javascript - 无法读取未定义的属性 'push'

c++ - 如何将多个字符串转换成同一个char数组?

android - 在 Kotlin 中对 ArrayList 进行排序

java - 数组、栈和队列的性能差异

node.js - 使用 Node.js 进行 MongoDB 操作时的回调