MongoDB remove object from array deletes complete array -
i have document one:
{ "_id" : 0, "name" : "aimee zank", "scores" : [ { "type" : "exam", "score" : 1.463179736705023 }, { "type" : "quiz", "score" : 11.78273309957772 }, { "type" : "homework", "score" : 6.676176060654615 }, { "type" : "homework", "score" : 35.8740349954354 } ] }
on document want delete score has lowest score of type homework. first, i'm trying on mongo shell, put values manually.
db.students.update({ _id:0}, {$unset: {"scores.score":6.676176060654615} })
that query nothing, tried search on google , found here question removing object array , tried other query:
db.students.update({ _id:0 }, { $unset: { "scores": { "homework": 6.676176060654615 } } }, false, true);
the second query worked, not expected because result was
{ "_id" : 0, "name" : "aimee zank" }
to check lowest value of type homework exists find query:
db.students.find({"scores.score":6.676176060654615}).pretty();
if want delete/pull score = 6.676176060654615
, need use following query :
db.collection.update({"_id":0},{"$pull":{"scores":{score: 6.676176060654615}}})
if want find minimum value , remove collection. need 2 steps. more details refer this
Comments
Post a Comment