简体   繁体   中英

Automatically update document in MongoDB?

I'm using MongoDB and Node.js. Let's say we have two users:

{
    "name": "user1",
    "email": "user1@email.com",
    "pairedWith": "user2@email.com",
    "paired": false,
}

and

{
    "name": "user2",
    "email": "user2@email.com",
    "pairedWith": "user1@email.com",
    "paired": false,
}

They can pair with each other by searching for the correct email address. After both users searched for the email and paired with each other, I want the paired field to become true automatically . Only after both users have paired. Of course here user1 must be paired with user2, and user2 must be paired with user1 because they specifically wanted that.

So how do I tell MongoDB to update paired to true automatically once 2 users are paired? Based on this true value, I will display something for both of them, but until it's not true, I can't.

you can add an additional field for entered_correct email. So, if user1 enters correct email set it to true. now, when user2 enters correct email for user1 set it to true.

both times check in another user's document if entered_correct is true there. When both documents have entered_correct true, set paired to true in both of them.

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