0

How can i remove a candid from the below array collection.

{
    "_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")
        }
    ]
}

expected output is

{
    "_id" : ObjectId("58978989"),
    "positionId" : "54535343",
    "jobTitle" : "Developer",
    "status" : "Open",
    "jobDescription" : "HyperLink Place holder",
    "candidate" : [ 
        {
            "candid" : ObjectId("b20474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b30474567892345678900021")
        }, 
        {
            "candid" : ObjectId("b40474567892345678900021")
        }
    ]
}
R.A. Lucas
  • 1,121
  • 1
  • 12
  • 17
Harish K
  • 11
  • 5

1 Answers1

-1

Since the array contains identical objects I assume you want to delete a specific index. If so then use the following.

db.test.update({_id: ObjectId("58978989")}, {$unset: {"candidate.1": 1}})
db.test.update( { _id: ObjectId("58978989")}, { $pull: { candidate: null} } )

Have a look at this to learn more https://docs.mongodb.com/manual/reference/operator/update/pull/

SJFJ
  • 657
  • 6
  • 18
  • This is will result in the document you wanted. If this is not helping you please provide more info so we can help you. – SJFJ Oct 25 '16 at 07:32
  • the above query is working fine in robomongo but when i was trying to run the same query using model in my node app it is not running – Harish K Oct 27 '16 at 11:04