search

Home  >  Q&A  >  body text

MongoDb,如何根据某个_id值删除数据

根据自带_id值删除老有问题,
javascript collection.remove({_id: ObjectId(id)}, {w:1}, function (err) { db.close() callback(err) })
提示ObjectId未定义,直接写_id:id 删不掉,id为一个字符串

PHPzPHPz2802 days ago723

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-04-25 09:03:59

    Read the readme document of the official module,
    Need to first var ObjectID = require('mongodb').ObjectID;
    Then use collection.findAndRemove({_id: new ObjectID(id)}) to do this

    reply
    0
  • PHP中文网

    PHP中文网2017-04-25 09:03:59

    db.collection.remove({"_id":ObjectId(id)});

    reply
    0
  • 怪我咯

    怪我咯2017-04-25 09:03:59

    If the id is a string, you can do this directly

    db.collection.remove({"_id":"你的id"});
    

    reply
    0
  • Cancelreply