Remove by _id in MongoDB console

前端 未结 11 1254
野的像风
野的像风 2020-12-22 16:14

In the MongoDB console how can I remove a record by id? Here\'s my collection :

[ 
  {
     \"_id\" : { \"$oid\" : \"4d512b45cc9374271b02ec4f\" },
     \"nam         


        
11条回答
  •  盖世英雄少女心
    2020-12-22 17:15

    Suppose we have this dummy collection:

    { "_id" : ObjectId("5ea53fedaa79db20d4e14284"), "item" : "planner", "qty" : 75 }
    

    simply use:

    db.inventory.deleteOne({ _id: ObjectId("5ea53fedaa79db20d4e14284") })
    

    it will be deleted with this as a response:

    { "acknowledged" : true, "deletedCount" : 1 }
    

    Thats it.

提交回复
热议问题