如何在复杂的Collection-Object中附加新文档

时间:2015-11-20 08:12:42

标签: mongodb database

我有一个集合:

{
    "_id": ObjectId("503b83dfad79cc8d26000004"),
    "uid": "9a8a2c5b9418cc1baadfa41255791414",
    "link": "http://stackoverflow.com/questions",
    "tasks": [
      {
        "query": "lorem lorem",
        "list": [
          {
            "timestamp": "159",
            "provider": "Lor Em"
          }
        ]
      }
    ]
}

每个集合元素都包含TASKS数组。每个TASK元素都包含Query + List-array。

我必须追加新的TASK[index].LIST-object(见图)

enter image description here

但我无法理解,如何查看CASE_TO_FIND_ELEMENT中的全部内容:

db.myCollection.update(
    {<CASE_TO_FIND_ELEMENT>},
    {
        $push: {
            list: {
                timestamp: "1233",
                provider: "myNewProvier"
            }                
        }
    },
    {
        upsert: true,
        multi: false
    }
)

更新
newQuery -> any string中使用的document.tasks[index].query(例如来自服务器) 我在代码中创建的这个值必须检查它们是否存在于DB中:
uid + link + (query + (TIMESTAMP + PROVIDER))

更新v2

> db.demo.find().pretty()
{
    "_id" : ObjectId("564f04f4be5d6f2f98c77dcd"),
    "uid" : "id3",
    "task" : [
            {
                    "list" : {
                            "lid" : "lX",
                            "ltext" : "demoX"
                    }
            }
    ]
}

在列表数组UID == "id3"中搜索"lid" == "lX"和任务数组,如果找不到,请在task.list-array中推送新文档:

 db.demo.update({
     "uid": "id3",
     "task.list.lid": "lX"
 }, {
     $push: {
         "task": {
             "list": {
                 "lid": "lX2",
                 "ltext": "demoX2"
             }
         }
     }
 }, true, false)

但在这种情况下,mongo在数组中插入一个新文档:(

> db.demo.find().pretty()
{
        "_id" : ObjectId("564f04f4be5d6f2f98c77dcd"),
        "uid" : "id3",
        "task" : [
                {
                        "list" : {
                                "lid" : "lX",
                                "ltext" : "demoX"
                        }
                },
                {
                        "list" : {
                                "lid" : "lX2",
                                "ltext" : "demoX2"
                        }
                },
                {
                        "list" : {
                                "lid" : "lX2",
                                "ltext" : "demoX2"
                        }
                }
        ]
}

您可以帮我在MongoDB集合中添加一些数据吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

猜猜你想要什么。

这是原始文件

{"_id": ObjectId("503b83dfad79cc8d26000004"),
"uid": "9a8a2c5b9418cc1baadfa41255791414",
"link": "http://stackoverflow.com/questions",
"tasks": [
  {
    "query": "lorem lorem",
    "list": [
      {
        "timestamp": "159",
        "provider": "Lor Em"
      }
    ]
  }
]}

你可以这样做:

db.demo.update({"tasks": { $elemMatch: { "query": "lorem lorem" }}}, {$push: {"tasks.$.list": {"timestamp": "160", "provider": "Lor Em"}}});

您将在更新的文档中看到此内容:

{"_id" : ObjectId("588b8ff15a66b2e3316ce70a"),
"uid" : "9a8a2c5b9418cc1baadfa41255791414",
"link" : "http://stackoverflow.com/questions",
"tasks" : [
    {
        "query" : "lorem lorem",
        "list" : [
            {
                "timestamp" : "159",
                "provider" : "Lor Em"
            },
            {
                "timestamp" : "160",
                "provider" : "Lor Em"
            }
        ]
    }
]}