How to remove a document referenced by an id in mongoDB from php?

前端 未结 3 1244
眼角桃花
眼角桃花 2020-12-11 13:34

I was successful in deleting documents using other fields but could not delete using \"_id\" field. The PHP page says that the id should be a string (which it is by default)

3条回答
  •  [愿得一人]
    2020-12-11 14:24

    "To remove a document based on its ID, you need to ensure that you pass the ID as a MongoID object rather than just a string:"

    Normally what the PHP manual states is true but not for you. You have changed the type of your _id to something other than an ObjectId (aka a MongoId).

    With this in mind you need to search by that other object which is a MongoInt32:

    $db->collection->remove(array('_id'=>new MongoInt32(1)))
    

提交回复
热议问题