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

查看:96
本文介绍了如果数组存在,则将对象推入数组,否则在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
         }
    ]
}

我该如何实现?

推荐答案

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

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

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

If your document to alter looks like this:

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

然后像这样更新语句:

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": ""
        }
    ]
}

您所做的基本相同:

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
}

然后通过在查询条件中进行测试并使用

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"表示第一个数组元素,这不是true).结果与原始结果相同:

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天全站免登陆