简体   繁体   中英

Update collection based on data in another collection in MongoDB

I have two MongoDB collections. questions :

{
    "_id" : "8735574",
    "title" : "...",
    "owner" : {
        "user_id" : 950690
    },
}
{
    "_id" : "8736808",
    "title" : "...",
    "owner" : {
        "user_id" : 657258
    },
}

and users :

{
    "_id" : 950690,
    "updated" : SomeDate,
    ...
}
{
    "_id" : 657258,
    "updated" : SomeDate,
    ...
}

The entries in users have to be regularily created or updated based on questions . So I would like to get all the user_id s from questions that either do not have an entry in users at all or their entry in users was updated more than eg one day ago.

To achieve this, I could read all user_id s from questions and then manually drop all users from the result that do not have to be updated. But this seems to be a bad solution as it reads a lot of unneccessary data. Is there a way to solve this differently? Some kind of collection join would be great but I know that this does not (really) exist in MongoDB. Any suggestions?

PS: Nesting these collections into a single collection is no solution as users has to be referenced from elsewhere as well.

Unfortunately there is no good way of doing this and since you don't have access to the indexes to able to do this client side without reading out all the data and manually manipulating it, it is the only way.

The join from users to questions could be done by querying the users collection and then doing an $in on the questions collection but that's really the only optimisation that can be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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