Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从MongoDb中数组的数组中删除元素_Arrays_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

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

Arrays 从MongoDb中数组的数组中删除元素,arrays,mongodb,mongoose,mongodb-query,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("565eb481bf35eeb83d7f9f

下面是联系人数组的架构。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?

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

db.collection.update(
    { "contacts.hashtag": "openLove" },
    {
        "$pull": {
            "contacts.$.hashtag": "openLove"
        }
    }
)
但是,由于这只支持一级深度数组(位置数组充当与查询文档匹配的第一个元素的占位符),因此仅删除与查询文档匹配的第一个元素。这里有一个可跟踪的JIRA:

如果您知道hashtags数组的索引,该索引中包含要事先删除的元素,那么更新查询将是:

db.collection.update(
    { "contacts.hashtag": "openLove" },
    {
        "$pull": {
            "contacts.0.hashtag": "openLove",
            "contacts.1.hashtag": "openLove"
        }
    }
)
请考虑重新设计架构以避免嵌套数组,这样您就可以通过创建一个单独的contacts集合来规范集合,其中每个文档表示一个联系人,其中的信息与原始集合中复制的一组联系人相同。如下所示:

集合架构:

{
    _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")
}
联系人模式:

{
    _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" }
    }
)
如果重新设计模式超出了您的范围,那么您需要一种机制来动态生成更新文档,即动态创建对象。考虑使用<强> <强>来生成,这个<强> <强>详细说明整个操作概念。

< P>在回答的帮助下,我通过使用每个联系人的对象ID来工作。

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