How to query MongoDB to test if an item exists?

后端 未结 9 1523
囚心锁ツ
囚心锁ツ 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:25

    filter_dict = {"key":value}
    if db.collection.count_documents(filter_dict):
        print("item is existed")
    else:
        print("item is not existed")
    

提交回复
热议问题