【问题标题】:How do I add an array of elements in MongoDB to an array in an existing document?如何将 MongoDB 中的元素数组添加到现有文档中的数组中?
【发布时间】:2016-04-22 23:38:37
【问题描述】:

在 MongoDB 中,我正在尝试编写一个查询来将数组中的元素添加到现有文档中,而不是将元素添加为对象:

property: ObjectID(xxx)

元素正在被添加

ObjectID(xxx)

如果我弄错了术语,请原谅我。我对 MongoDB 完全陌生;我通常只使用关系数据库。如何正确添加这些新元素?

我有一个名为拍卖的集合,它有两个字段:ID 和属性。属性是一个名为属性的对象数组。这是一个包含两个拍卖文件的示例:

** 我更改了对象 ID,以便在我们的讨论中更容易引用它们

Collection db.auctions
{
    "_id" : ObjectId("abc"),
    "properties" : [
        {
            "property" : ObjectId("prop1")
        }, 
        {
            "property" : ObjectId("prop2")
        }, 
        {
            "property" : ObjectId("prop3")
        }]
}
{
    "_id" : ObjectId("def"),
    "properties" : [
        {
            "property" : ObjectId("prop97")
        }, 
        {
            "property" : ObjectId("prop98")
        }]
}

我想添加 3 个新属性来拍卖“abc”。我该怎么做?

这是我尝试过的:

我有一个如下所示的属性数组:

Array PropsToAdd
[
    ObjectId("prop4"), 
    ObjectId("prop5"), 
    ObjectId("prop6")
]

我编写了一个更新查询来将这些属性推送到拍卖中的属性数组中:

db.auctions.update(
    {"_id": "abc"}
    ,
    { $push: { properties: { $each: PropsToAdd } } } 
);

这个查询给出了下面的结果。请注意,不是使用我的数组中的值添加命名属性的元素,而是添加我的数组中的值。我显然需要添加那个“属性”部分,但我该怎么做呢?

Collection db.auctions (_id "abc" only)
    {
    "_id" : ObjectId("abc"),
    "properties" : [
        {
            "property" : ObjectId("prop1")
        }, 
        {
            "property" : ObjectId("prop2")
        }, 
        {
            "property" : ObjectId("prop3")
        }, 
        ObjectId("prop4"), 
        ObjectId("prop5"), 
        ObjectId("prop6"), 
        ObjectId("prop7")]
}

我要找的结果是这样的:

Collection db.auctions (_id "abc" only)
{
    "_id" : ObjectId("abc"),
    "properties" : [
        {
            "property" : ObjectId("prop1")
        }, 
        {
            "property" : ObjectId("prop2")
        }, 
        {
            "property" : ObjectId("prop3")
        }, 
        {
            "property" : ObjectId("prop4")
        },
        {
            "property" : ObjectId("prop5")
        },         
        {
            "property" : ObjectId("prop6")
        } 
}

以下是有关我要添加的属性数组的更多信息。我通过运行这些查询得到它。也许其中一个需要改变?

此查询获取当前属性的数组:

var oldActiveProperties = db.properties.distinct( "saleNumber", { "active": true, "auction": ObjectId("abc") } );

然后这些结果用于在新文件中查找旧文件中没有的属性:

var PropsToAdd = db.newProperties.distinct(
    "_id"
    , { "saleNumber": { "$nin": oldActiveProperties }, "active": true}
);

结果数组是我需要添加到拍卖集合中的。

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    使用 JavaScript 的原生 map() 方法将数组映射到文档数组。以下显示了这一点:

    var PropsToAdd = db.newProperties.distinct("_id", 
        { "saleNumber": { "$nin": oldActiveProperties }, "active": true}
    ).map(function (p) { return { property: p }; });
    
    db.auctions.update(
        {"_id": "abc"},
        { $push: { "properties": { "$each": PropsToAdd } } } 
    );
    

    【讨论】:

    • 效果很好;谢谢你!就像我说的,我是 MongoDB 的新手。我正在使用 MongoChef 编写查询。我在编写查询时是否可以使用大多数/所有 JavaScript 函数?
    • MongoDB 使用 JavaScript 引擎 SpiderMonkey 用于 mongo shell 和 mongod 中的服务器端 javascript,因此在 mongo shell 中编写查询时 API 可用,尽管与 MongoDB 的大多数交互不要使用 JavaScript,而是使用交互应用程序语言中的 idiomatic driver
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2017-09-16
    • 2019-01-04
    • 2020-01-26
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多