如果数组存在,则将对象推入数组,否则在 MongoDB 中使用对象创建数组 [英] Push object into array if the array exists otherwise create the array with object in MongoDB

查看:21
本文介绍了如果数组存在,则将对象推入数组,否则在 MongoDB 中使用对象创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个文档的集合:

I have collection with a document like this:

{
    _id : "abcd",
    name : "Tom",
    myArray : [
        {
            field1 : "",
            field2 : ""
        }
    ]
},
{
    _id : "efgh",
    name : "Jerry"
}

我有一个用于 myArray 的新对象.我想编写一个查询来仅更新一个文档.

I have a new object for myArray. I want to write a query to update only one document.

如果查询匹配具有 _id : "abcd" 的文档,则将新对象推送到 myArray 字段:

If the query matches the document with _id : "abcd", then push the new object in to myArray field:

{
    _id : "abcd",
    name : "Tom",
    myArray : [
        {
            field1 : "",
            field2 : ""
        },
        {
            // new object
        }
    ]
}

如果查询与 _id : "efgh" 匹配,则创建字段 myArray 并在其中包含新对象:

And if the query matches with _id : "efgh" create the field myArray with new object inside it:

{
    _id : "efgh",
    name : "Jerry"
    myArray : [
         {
              // new object
         }
    ]
}

我怎样才能做到这一点?

How can I achieve this?

推荐答案

为了解释这里所有可能的情况,请考虑每个文档情况:

To explain all the possible cases here, the consider each document case:

如果您要更改的文档如下所示:

If your document to alter looks like this:

{
    "_id": "efgh",
    "name": "Jerry"
}

然后是这样的更新语句:

Then an update statment like this:

db.collection.update(
    { "_id": "efgh" },
    { "$push": { "myArray": { "field1": "abc", "field2": "def" } } }
)

结果如下:

{
    "_id": "efgh",
    "name": "Jerry",
    "myArray": [
        {
            "field1": "abc",
            "field2": "def"
        }
    ]
}

因此创建了数组并附加了新项目.

So the array is created and the new item appended.

如果您的文档已经有这样的数组:

If your document already has an array like this:

{
    "_id": "abcd",
    "name": "Tom",
    "myArray": [
        {
            "field1": "",
            "field2": ""
        }
    ]
}

而且你做了基本相同的声明:

And you do basically the same statement:

db.collection.update(
    { "_id": "abcd" },
    { "$push": { "myArray": { "field1": "abc", "field2": "def" } } }
)

然后将新的文档内容追加到现有数组中:

Then the new document content is appended to the existing array:

{
    "_id": "abcd",
    "name": "Tom",
    "myArray": [
        {
            "field1": "",
            "field2": ""
        },
        {
            "field1": "abc",
            "field2": "def"
        }
    ]
}

然而,如果您的原始文档具有命名字段但它不是数组,则如下所示:

If however your original document has the named field but it is not an array, like this:

{
    "_id": "efgh",
    "name": "Jerry",
    "myArray": 123
}

然后通过在查询条件中进行测试并使用 $set 代替:

Then make sure it is not an array by testing in the query condtion and using $set instead:

db.collection.update(
    { "_id": "efgh", "myArray.0": { "$exists": false } },
    { "$set": { "myArray": [{ "field1": "abc", "field2": "def" }] } }
)

这将使用包含您的内容的新数组安全地覆盖不是数组的元素(点符号myArray.0"表示第一个数组元素,这不是真的).结果和原来一样:

That will safely overwrite the element that is not an array ( dot notation "myArray.0" means first array element, which is not true ) with a new array containing your content. The result is the same as the original:

{
    "_id": "efgh",
    "name": "Jerry",
    "myArray": [
        {
            "field1": "abc",
            "field2": "def"
        }
    ]
}

这篇关于如果数组存在,则将对象推入数组,否则在 MongoDB 中使用对象创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆