0 votes
1 view
in Web Technology by (42k points)

I want to remove a record by id in Mongo console. The following is my collection :

  {

     "_id" : { "$oid" : "4d512b45cc9374271b02ec4f" },

     "name" : "Gazza"

  },

  {

     "_id" : { "$oid" : "4d513345cc9374271b02ec6c" },

     "name" : "Dave",

     "adminOf" : { },

     "email" : "[email protected]"

  }

]

And the following are the commands that I have implemented but it didn't work :

db.test_users.remove( {"_id":{"$oid":new ObjectId("4d512b45cc9374271b02ec4f")}});

db.test_users.remove( {"_id":{"$oid":"4d513345cc9374271b02ec6c"}});

db.test_users.remove( {"_id":"4d512b45cc9374271b02ec4f"});

db.test_users.remove( {"_id":new ObjectId("4d512b45cc9374271b02ec4f")});

1 Answer

0 votes
by (90.3k points)

In MongoDB, you don’t have to add new ObjectId, just write the following code:

db.test_users.remove( {"_id": ObjectId("4d512b45cc9374271b02ec4f")});

The general syntax is:

db.collectionName.remove( {"_id": ObjectId("your_hexadecimal_value_for_the_field")});

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...