简体   繁体   中英

Update data in a MongoDB collection when certain data's gets deleted from another collection but both collection under same cluster

Is it possible to update a certain field in one collection in MongoDB when a certain data's timestamps expire in another collection under the same cluster? If it can be done, could you provide an example?

Example of what I am trying to accomplish: collection1: [{ name:"ABC", Age:26, value:true }]

Collection2: [{ name:"ABC", result:"pass" }]

So, if the data with the name ABC expires in collection2, I want to change the value of ABC to false in collection1.

I know this is not exactly what you want but by this query, you could get the same results without updating col1 use aggregate and join two collection if there is not any data in col2 for specific name return value as false else true

db.col1.aggregate([
    {$lookup:{
        from:"col2",
        localField:"name",
        foreignField :"name",
        as:"t"
    }},
    {
        $project:{
            name:1,
            Age:1,
            value:{$cond:{ if: { $eq: [ [], "$t" ] }, then: false, else: true }}
        }
    }
])

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