如何查询MongoDB来测试一个项目是否存在? [英] How to query MongoDB to test if an item exists?

查看:100
本文介绍了如何查询MongoDB来测试一个项目是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB是否提供查找或查询方法来根据任何字段值测试项目是否存在?我们只是想检查是否存在,而不是返回该项目的全部内容。

解决方案

我不相信有一个直接的方式通过它的值检查项目的存在。但你可以通过只检索ID(选择字段

  db.your_collection.find({.. criteria ..},{_id:1} ); 


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.

解决方案

I dont believe that there is a straight way of checking the existence of the item by its value. But you could do that by just retrieving only id (with field selection)

db.your_collection.find({..criteria..}, {"_id" : 1});

这篇关于如何查询MongoDB来测试一个项目是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆