简体   繁体   English

自动更新 MongoDB 中的文档?

[英]Automatically update document in MongoDB?

I'm using MongoDB and Node.js.我正在使用 MongoDB 和 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.它们可以通过搜索正确的 email 地址来相互配对。 After both users searched for the email and paired with each other, I want the paired field to become true automatically .两个用户搜索 email 并相互配对后,我希望paired字段自动变为true 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.当然,这里 user1 必须与 user2 配对,而 user2 必须与 user1 配对,因为他们特别想要这样。

So how do I tell MongoDB to update paired to true automatically once 2 users are paired?那么如何告诉 MongoDB 在 2 个用户配对后自动更新为 true呢? 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.您可以为输入的正确 email 添加一个附加字段。 So, if user1 enters correct email set it to true.因此,如果 user1 输入正确的 email 则将其设置为 true。 now, when user2 enters correct email for user1 set it to true.现在,当 user2 为 user1 输入正确的 email 时,将其设置为 true。

both times check in another user's document if entered_correct is true there.如果entered_correct 在那里,两次都签入另一个用户的文档。 When both documents have entered_correct true, set paired to true in both of them.当两个文档都输入_正确时,将它们都设置为 true。

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

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