更新mongodb中的多个文档

编程入门 行业动态 更新时间:2024-10-23 08:29:11
本文介绍了更新mongodb中的多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在schedule 集合中有一个特定用户的3个项目,在每个记录(文档)中,我都有一个字段isActive,user将设置为当他想从事该项目时可以为true,并且只能为一个项目设置.请参见以下记录:

I have 3 projects of a particular user in schedule collection and in every record(document), I have field isActive, which user will set to true when he wants to work on that project, and can set for only one project.See below records:

{ "_id" : ObjectId("5c1e0a1cd8a20917b8564e49"), "isActive" : true, "projectId" : ObjectId("5c0a2a8897e71a0d28b910ac"), "userId" : ObjectId("5c0a29e597e71a0d28b910aa"), "hours" : 1, }, { "_id" : ObjectId("5c1e0a1cd8a20917b8564e4a"), "isActive" : false, "projectId" : ObjectId("5c188a9959f6cf1258f4cb01"), "userId" : ObjectId("5c0a29e597e71a0d28b910aa"), "hours" : 7, }, { "_id" : ObjectId("5c1e0a1cd8a20917b8564e4b"), "isActive" : false, "projectId" : ObjectId("5cyt2a7797e71a0d25b930ad"), "userId" : ObjectId("5c0a29e597e71a0d28b910aa"), "hours" : 1, }

例如,用户已完成5c0a2a8897e71a0d28b910ac此 projectId 的工作.现在他想为5c188a9959f6cf1258f4cb01 projectId工作,所以我想编写更新查询以将5c188a9959f6cf1258f4cb01 projectId的isActive更新为true,对于5c0a2a8897e71a0d28b910ac和5cyt2a7797e71a0d25b930ad项目的isActive更新为false,意味着用户其他项目.

for example, User has completed his work for 5c0a2a8897e71a0d28b910ac this projectId. Now he wants to work for 5c188a9959f6cf1258f4cb01 projectId, So i want to write update query to update isActive to true for 5c188a9959f6cf1258f4cb01 projectId and isActive to false for 5c0a2a8897e71a0d28b910ac and for 5cyt2a7797e71a0d25b930ad projects, means user other projects.

推荐答案

因此,基本上,您必须使用两个查询,一个查询到$set isActive到false,另一个查询到$set isActive到true对于特定的userId

So basically you have to use two queries one to $set isActive to false and one to $set isActive to true for a particular userId

1)将isActive设置为true

Model.update( { "projectId": "5c188a9959f6cf1258f4cb01", "userId": "5c0a29e597e71a0d28b910aa" }, { "$set": { "isActive": true }} )

2)到$set对于同一userId

Model.update( { "projectId": { "$ne": "5c188a9959f6cf1258f4cb01" }, "userId": "5c0a29e597e71a0d28b910aa" }, { "$set": { "isActive": false }} )

更多推荐

更新mongodb中的多个文档

本文发布于:2023-11-22 18:34:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文档   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!