How to query MongoDB to test if an item exists?

后端 未结 9 1519
囚心锁ツ
囚心锁ツ 2020-11-27 04:06

Does MongoDB offer a find or query method to test if an item exists based on any field value? We just want check existence, not return the full contents of the item.

9条回答
  •  春和景丽
    2020-11-27 04:23

    An update to Xavier's answer:

    db.collection.countDocuments({}, { limit: 1 })
    

    Expects a callback as a second argument now, so this can be used instead:

    db.collection.countDocuments({}).limit(1)
    

提交回复
热议问题