'Mongoose Nodejs pull element from array by property of property
So here is my mongoose Object:
{
"_id" : ObjectId("626aaff13fa90efda9ea1975"),
"username" : "Raxo",
"password" : "089a9902261aa3d27218ef8ea5e4885a52cafc7278684978b0953dc6846f07a6",
"solved" : [
{
"challenge" : {
"_id" : ObjectId("627178fd7fff3bdc2ad941e6"),
"name" : "Challenge6315 ",
"category" : "crypto",
"flag" : "Nice try XD",
"hints" : [ "Easy Peasy Lemon Squeezy!" ],
"points" : 100,
"info" : "I am a challenge!",
"level" : 0, "solveCount" : 0,
"file" : "",
"__v" : 0
},
"timestamp" : 1651603748444
}],
"score" : 800,
"key" : "65b8457a-9c00-4829-8d18-ed2e19cef53",
"isAdmin" : true,
"teamId" : "62717c6a7b8c337ac509176a",
"__v" : 0
}
I am trying to delete the challenge inside the solved array using:
await users.updateMany({
solved: { $elemMatch: { 'challenge._id': challengeExists._id } }
}, {
$inc: { score: -challengeExists.points },
$pull: {solved: {"challenge._id": challengeExists._id}}
});
It does find the user successfully and remove points from the score but does not successfully pull the challenge, I cant seem to find what I am doing wrong. Any help appreciated
Also weirdly it works in the CLI with:
db.users.updateMany({ solved: { $elemMatch: { 'challenge._id': ObjectId("627191c0fcff360116a67528") } } }, { $pull: { solved: { "challenge._id": ObjectId("627191c0fcff360116a67528") } }, $inc: { score: -100 }, })
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
