简体   繁体   中英

mongodb / nodejs: findandmodify method not working

I have below json data and want to update the value according to the condition

{
    "_id" : ObjectId("5fce2d4c7b2ea2e79ercvju4"),
    "userId" : 1,
    "token": "jwt_token_value",
    "isActive" : 0,
    "__v" : 0
}
{
    "_id" : ObjectId("5fce2d4c7b2ea2e79ercvjk0"),
    "userId" : 1,
    "token": "jwt_token_value",
    "isActive" : 0,
    "__v" : 0
}
{
    "_id" : ObjectId("5fd0d45tjd82a02dd0f17fc4"),
    "userId" : 1,
    "token": "jwt_token_value",
    "isActive" : 1,
    "__v" : 0
}

I have managed the things as below.

let update = await UserDetails.findAndModify(
    { userId: 1, token: token },
    [],
    {$set: { isActive: 0 }},
    { new: true }
);

=> This query should update the last json collection key of isActive to 1. But it is not updating the values any how. What the things that I am doing wrong? It is not throwing me any error as well to debug.

I am following this answer: https://stackoverflow.com/a/24648693/5624578

You can include the query identifiers to specify what each section is doing like this:

let update = await UserDetails.findAndModify(
    query: { userId: 1, token: token },
    update: {$set: { isActive: 0 }},
    upsert: 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