简体   繁体   English

Mongodb从文档获取唯一值并添加到数组

[英]Mongodb get unique value from documents and add to array

I wanted to do a query to match documents in one collection with documents in another collection based upon a value which should be contained in both sets of documents but, as I have been informed that Mongo does not support a JOIN, I believe I can't do what I want in the way I want to. 我想根据两个文档集中都应包含的值进行查询,以将一个集合中的文档与另一个集合中的文档进行匹配,但是,由于得知Mongo不支持JOIN,我相信我可以用自己想要的方式做我想做的事。

My alternative method then is to insert a document into the collection (col1) where I want to do a query and update which contains an array of all the unique cycle number which are in the other collection (col2). 然后,我的替代方法是将文档插入到集合(col1)中,我要在其中进行查询和更新,其中包含另一个集合(col2)中所有唯一循环号的数组。

Collection 1 (Col 1) 收藏1(Col 1)

        {
        "_id" : ObjectId("5670961f910e1f54662c11ag"),
        "objectType" : "Account Balance",
        "Customer" : "Thomas Brown",
        "status" : "unprocessed",
        "cycle" : "1234"
    }, 
    {
        "_id" : ObjectId("5670961f910e1f54662c12fd"),
        "objectType" : "Account Balance",
        "Customer" : "Luke Underwood",
        "status" : "unprocessed",
        "cycle" : "1235"
    }

Collection 2 (Col 2) 收藏2(Col 2)

        {
        "_id" : ObjectId("5670961f910e1f54662c1d9d"),
        "objectOrigin" : "Xero",
        "Value" : "500.00",
        "key" : "grossprofit",
        "cycle" : "1234",
        "company" : "e56e09ef-5c7c-423e-b699-21469bd2ea00"
    }, 
    {
        "_id" : ObjectId("5670961f910e1f54662c1d9f"),
        "objectOrigin" : "Xero",
        "Value" : "500.00",
        "key" : "grossprofit",
        "cycle" : "1234",
        "company" : "0a514db8-1428-4da6-9225-0286dc2662c1"
    }, 
    {
        "_id" : ObjectId("5670961f910e1f54662c1da0"),
        "objectOrigin" : "Xero",
        "Value" : "-127.28",
        "key" : "grossprofit",
        "cycle" : "1234",
        "company" : "c2d0561c-dc5d-44b9-beaf-d69a3472a2b8"
    }, 
    {
        "_id" : ObjectId("5670961f910e1f54662c1da1"),
        "objectOrigin" : "Xero",
        "Value" : "-127.28",
        "key" : "grossprofit",
        "cycle" : "1235",
        "company" : "c3fbe6e4-962a-45f6-9ce3-71e2a588438c"
    }

So I want to create a document in collection 1 which looks like this: 所以我想在集合1中创建一个文档,如下所示:

    {
        "_id" : ObjectId("5670961f910e1f54662c1d9f"),
        "objectType" : "Status Updater",
        "cycles" : ["1234","1235"]
    }

Now what I want to do is query ALL documents where cycle = cycles and update "status" to "processed". 现在,我要做的是查询所有文档,这些文档中的cycle = cycles并将“状态”更新为“已处理”。 I believe I would do this with a findAndModify with multi : true but not entirely sure. 我相信我可以使用findAndModify与multi:true,但不是完全确定。

When finished, I will just simply delete any document in the Collection 1 where objectType is "Status Updater". 完成后,我将仅删除集合1中objectType为“ Status Updater”的任何文档。

If I understand correctly, you want to 如果我理解正确,您想

  • a) update all documents in collection #1 where the value of cycle exists in collection #2. a)更新集合1中的所有文档,其中集合2中存在cycle值。
  • b) Furthermore, your document of type "objectType" : "Status Updater" is only a temporary document to keep track of all the cycle values. b)此外,您的"objectType" : "Status Updater"类型的文档"objectType" : "Status Updater"只是一个临时文档,用于跟踪所有循环值。

I think you can skip b) and just use the following (this code needs to be executed in the mongo shell): 我认为您可以跳过b)并仅使用以下代码(此代码需要在mongo shell中执行):

# get values of all unique cycle values
# returns an array containing: ["1234", "1235", ...]
values = db.coll2.distinct("cycles", {})

# find all documents where cycles value is in the values array 
# and update their status.
db.coll1.update({cycles: {$in: values}}, {$set: {status: "processed"}}, {multi: true})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM