Reputation:
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,
}
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.
Upvotes: 0
Views: 137
Reputation: 46441
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) to set isActive
to true
Model.update(
{ "projectId": "5c188a9959f6cf1258f4cb01", "userId": "5c0a29e597e71a0d28b910aa" },
{ "$set": { "isActive": true }}
)
2) to $set
isActive to false
for other projectId
for the same userId
Model.update(
{ "projectId": { "$ne": "5c188a9959f6cf1258f4cb01" }, "userId": "5c0a29e597e71a0d28b910aa" },
{ "$set": { "isActive": false }}
)
Upvotes: 1