Wie kann ich einen Kandidaten aus der untenstehenden Array-Sammlung entfernen?
{
"_id" : ObjectId("58978989"),
"positionId" : "54535343",
"jobTitle" : "Developer",
"status" : "Open",
"jobDescription" : "HyperLink Place holder",
"candidate" : [
{
"candid" : ObjectId("b20474567892345678900021")
},
{
"candid" : ObjectId("b30474567892345678900021")
},
{
"candid" : ObjectId("b40474567892345678900021")
},
{
"candid" : ObjectId("b50474567892345678900021")
}
]
}
erwartete Ausgabe ist
{
"_id" : ObjectId("58978989"),
"positionId" : "54535343",
"jobTitle" : "Developer",
"status" : "Open",
"jobDescription" : "HyperLink Place holder",
"candidate" : [
{
"candid" : ObjectId("b20474567892345678900021")
},
{
"candid" : ObjectId("b30474567892345678900021")
},
{
"candid" : ObjectId("b40474567892345678900021")
}
]
}
Antworten:
-1 für die Antwort № 1Da das Array identische Objekte enthält, gehe ich davon aus, dass Sie einen bestimmten Index löschen möchten. Wenn ja, dann benutzen Sie folgendes.
db.test.update({_id: ObjectId("58978989")}, {$unset: {"candidate.1": 1}})
db.test.update( { _id: ObjectId("58978989")}, { $pull: { candidate: null} } )
Schauen Sie sich das an, um mehr zu erfahren https://docs.mongodb.com/manual/reference/operator/update/pull/