Reputation: 173
How do I pull a value from a collection in Mongo?
This is the query I'ved tried used to pull one of the values in one of the collections.
I am trying to delete a value in the array in the collection srs.5689f5206f404774fc350407
db.dbName.update(
{ _id: ObjectId("5689e240d3235cbef83e41d0") },
{ $pull:{ 'srs.5689f5206f404774fc350407.$.flashcardID':
ObjectId("5689f52e6f404774fc35040a")}
})
This is the document in question.
{
"_id":ObjectId("5689e240d3235cbef83e41d0"),
"username":"lol1",
"score":"",
"decks":[
"5689e246d3235cbef83e41d1"
],
"srs":{
"5689e246d3235cbef83e41d1":[
{
"timer":1451876940132,
"flashcardID":ObjectId("5689e24cd3235cbef83e41d2"),
"correct":0,
"type":"srs"
},
{
"timer":1451876944421,
"flashcardID":ObjectId("5689e250d3235cbef83e41d3"),
"correct":0,
"type":"srs"
},
{
"timer":1451876949586,
"flashcardID":ObjectId("5689e255d3235cbef83e41d4"),
"correct":0,
"type":"srs"
},
{
"timer":1451876954478,
"flashcardID":ObjectId("5689e25ad3235cbef83e41d5"),
"correct":0,
"type":"srs"
},
{
"timer":1451876957760,
"flashcardID":ObjectId("5689e25dd3235cbef83e41d6"),
"correct":0,
"type":"srs"
}
]
},
"type":"user"
}
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: srs.5689f5206f404774fc350407.$.flashcardID"
}
})
I would like some advice on what to do. I've tried fixing this query for quite awhile.
Upvotes: 1
Views: 235
Reputation: 946
db.dbName.update({
_id: "5689e240d3235cbef83e41d0"
}, {
$pull: {
"srs.5689e246d3235cbef83e41d1": {
"flashcardID": "5689e24cd3235cbef83e41d2"
}
}
})
Try this. You need to tell what you want to pull that is the first key after "$pull" in this case "srs.5689e246d3235cbef83e41d1" then the where clause to find your value ""flashcardID": "5689e24cd3235cbef83e41d2"". Read the documentation https://docs.mongodb.org/manual/reference/operator/update/pull/
Upvotes: 3
Reputation: 3748
It appears the schema is not using an objectid
-- but a string. unless pretty printed...
$
is not a operator. you may be thinking of the $in
operator. in this case it seems it is not needed just use the flashcard string.
db.dbName.update( { _id: "5689e240d3235cbef83e41d0" }, { $pull: {"srs.5689f5206f404774fc350407": {"flashcardID": "5689f52e6f404774fc35040a"}}} )
Upvotes: 1