Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k 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 (108k points)
edited by

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")});

Learn about full stack web development by signing up for this professional Full Stack Training offered by Intellipaat.. 

Related questions

Browse Categories

...