This checks the document for the existence of the fields in the specified collection.

这将检查文档中指定集合中是否存在字段。

The syntax is

语法是

{ field: { $exists: <boolean> } }

The operator accepts the boolean values of either true or false.

运算符接受布尔值true或false。

If the boolean value is set to true, the exists operator matches the documents that contain the fields specified in the input parameters. If the boolean option is set to false the query returns the documents which do not contain the field.

如果布尔值设置为true,则exist操作符将匹配包含输入参数中指定的字段的文档。 如果布尔选项设置为false,则查询将返回不包含该字段的文档。

Let’s check out the examples of usage of exists operator.

我们来看一下存在运算符的用法示例。

存在运算符设置为true (Exists operator set to true)

This operation returns only the documents that contain the field specified in the query.

此操作仅返回包含查询中指定的字段的文档。

>db.car.find({ regno:{ $exists:true}})
{ "_id" : 51, "name" : "NissanSunny", "cno" : "H678", "regno" : 141, "speed" : 25 }
{ "_id" : 52, "name" : "Fiat", "cno" : "H679", "regno" : 142, "speed" : 57 }

The documents having the regno field are fetched when exists is set to true.

当存在设置为true时,将获取具有regno字段的文档。

将运算符设置为true,并指定选择标准 (Exists operator set to true and selection criteria specified)

This operation returns only the documents which satisfy the criteria entered and contain the fields specified in the query.

此操作仅返回满足输入条件并包含查询中指定字段的文档。

>db.car.find({ speed:{ $exists:true , $gt:80 }}){ "_id" : ObjectId("5474896b93f400069d439c04"), "name" : "Micra", "color" : "Lime", "cno" : "H186", "mfdcountry" : "Ethopia", "speed" : 84 }
{ "_id" : ObjectId("5474896b93f400069d439c03"), "name" : "Palio", "color" : "Purple", "cno" : "H183", "mfdcountry" : "Venice", "speed" : 82 }

The documents having the speed field and the speed greater than 80 are retrieved from the collection.

从集合中检索速度字段和速度大于80的文档。

存在运算符设置为false (Exists operator set to false)

This retrieves the documents that do not contain the fields specified in the query.

这将检索不包含查询中指定的字段的文档。

>db.car.find( { mfdcountry: { $exists:false}}){ "_id" : 8, "name" : "Zen", "speed" : 54 }
{ "_id" : ObjectId("5474896b93f400069d439c00"), "name" : "Indica", "color" : "Silver", "cno" : "H154" }
{ "_id" : 43, "name" : "Astar", "speed" : 79 }
{ "_id" : 51, "name" : "NissanSunny", "cno" : "H678", "regno" : 141, "speed" : 25 }
{ "_id" : 52, "name" : "Fiat", "cno" : "H679", "regno" : 142, "speed" : 57 }
{ "_id" : 59, "name" : "Quanta-45", "cno" : null, "regno" : null, "speed" : null }
{ "_id" : 99, "name" : "Brio", "cno" : null, "regno" : null, "speed" : null }

检索具有空值的文档 (Retrieve documents having null values)

This retrieves the documents that contains the null values for the field specified in the query.

这将检索包含查询中指定字段的空值的文档。

>db.car.find( { speed: { $exists:true } }){ "_id" : 8, "name" : "Zen", "speed" : 54 }
{ "_id" : 6, "name" : "HondaCity", "color" : "Grey", "cno" : "H106", "mfdcountry" : "Sweden", "speed" : 45 }
{ "_id" : ObjectId("5474642dd785e3a05a1808a7"), "name" : "Punto", "color" : "Wine Red", "cno" : "H109", "mfdcountry" : "Paris", "speed" : 45 }
{ "_id" : 9, "name" : "SwiftDezire", "color" : "Maroon", "cno" : "H108", "mfdcountry" : "New York", "speed" : 40 }
{ "_id" : 51, "name" : "NissanSunny", "cno" : "H678", "regno" : 141, "speed" : 25 }
{ "_id" : 99, "name" : "Brio", "cno" : null, "regno" : null, "speed" : null }
{ "_id" : 59, "name" : "Quanta-45", "cno" : null, "regno" : null, "speed" : null }

This operation retrieves the document having null values for speed field.

此操作检索速度字段具有空值的文档。

适用于现有操作的MongoDB Java程序 (MongoDB Java Program for exists operations)

Let’s now write a java program to check whether the fields exists in the MongoDB.

现在,让我们编写一个Java程序来检查MongoDB中是否存在这些字段。

package com.journaldev.mongodb;import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;public class MongoDBExists {// method to check whether the field existspublic static void existstrue() throws UnknownHostException {// Get a db connectionMongoClient m1 = new MongoClient("localhost");// connect to test db,use your own hereDB db = m1.getDB("test");// obtain the car collectionDBCollection coll = db.getCollection("car");// checking whether regno field exists by setting exists to trueDBObject query = new BasicDBObject("regno", new BasicDBObject("$exists", true));// store the documents in cursor carDBCursor car = coll.find(query);// iterate and print the contents of cursortry {while (car.hasNext()) {System.out.println(car.next());}} finally {// close the cursorcar.close();}}// method to check the fields along along with the criteria enteredpublic static void existstruewithcriteria() throws UnknownHostException {MongoClient m1 = new MongoClient("localhost");DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");// check whether speed fields exists and speed greater than 80DBObject query = new BasicDBObject("speed", new BasicDBObject("$exists", true).append("$gt", 80));DBCursor car = coll.find(query);System.out.println("-----------------------------------");try {while (car.hasNext()) {System.out.println(car.next());}} finally {car.close();}}// method to check the field does not existpublic static void existsfalse() throws UnknownHostException {MongoClient m1 = new MongoClient("localhost");DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");// checking for mfdcountry field by setting exists to falseDBObject query = new BasicDBObject("mfdcountry", new BasicDBObject("$exists", false));DBCursor car = coll.find(query);System.out.println("-------------------------------------------------");try {while (car.hasNext()) {System.out.println(car.next());}} finally {car.close();}}public static void existstruewithnullvalues() throws UnknownHostException {MongoClient m1 = new MongoClient("localhost");DB db = m1.getDB("test");DBCollection coll = db.getCollection("car");// returns documents with null valuesDBObject query = new BasicDBObject("regno", new BasicDBObject("$exists", true));DBCursor car = coll.find(query);System.out.println("---------------------------------------------");try {while (car.hasNext()) {System.out.println(car.next());}} finally {car.close();}}public static void main(String[] args) throws UnknownHostException {// invoking all the methodsexiststrue();existstruewithcriteria();existsfalse();existstruewithnullvalues();}}

Output of the above program is:

上面程序的输出是:

{ "_id" : 51.0 , "name" : "NissanSunny" , "cno" : "H678" , "regno" : 141.0 , "speed" : 25.0}
{ "_id" : 52.0 , "name" : "Fiat" , "cno" : "H679" , "regno" : 142.0 , "speed" : 57.0}
--------------------------------------------------------------------------------------------------------------
{ "_id" : { "$oid" : "5474896b93f400069d439c04"} , "name" : "Micra" , "color" : "Lime" , "cno" : "H186" , "mfdcountry" : "Ethopia" , "speed" : 84}
{ "_id" : { "$oid" : "5474896b93f400069d439c03"} , "name" : "Palio" , "color" : "Purple" , "cno" : "H183" , "mfdcountry" : "Venice" , "speed" : 82}
------------------------------------------------------------------------------------------------------------------
{ "_id" : 8.0 , "name" : "Zen" , "speed" : 54.0}
{ "_id" : { "$oid" : "5474896b93f400069d439c00"} , "name" : "Indica" , "color" : "Silver" , "cno" : "H154"}
{ "_id" : 43 , "name" : "Astar" , "speed" : 79}
{ "_id" : 51.0 , "name" : "NissanSunny" , "cno" : "H678" , "regno" : 141.0 , "speed" : 25.0}
{ "_id" : 52.0 , "name" : "Fiat" , "cno" : "H679" , "regno" : 142.0 , "speed" : 57.0}
---------------------------------------------------------------------------------------------------------------
{ "_id" : 51.0 , "name" : "NissanSunny" , "cno" : "H678" , "regno" : 141.0 , "speed" : 25.0}
{ "_id" : 52.0 , "name" : "Fiat" , "cno" : "H679" , "regno" : 142.0 , "speed" : 57.0}
{ "_id" : 59.0 , "name" : "Quanta-45" , "cno" :  null  , "regno" :  null  , "speed" :  null }
{ "_id" : 99.0 , "name" : "Brio" , "cno" :  null  , "regno" :  null  , "speed" :  null }

MongoDB exists is a simple operation to understand and it can help us in selecting only specific documents that have the given field.

MongoDB存在是一个易于理解的操作,它可以帮助我们选择具有给定字段的特定文档。

翻译自: https://www.journaldev.com/6330/mongodb-exists-example-mongo-shell-java-driver

MongoDB存在使用Mongo Shell和Java驱动程序的示例相关推荐

  1. 使用Mongo Shell和Java驱动程序的MongoDB Map Reduce示例

    Map Reduce is a data processing technique that condenses large volumes of data into aggregated resul ...

  2. 使用Mongo Shell和Java驱动程序删除MongoDB的示例

    MongoDB remove method removes a single document or all the documents present in the collection or th ...

  3. 使用Shell和Java驱动程序的MongoDB身份验证配置示例

    Authentication enables user to verify identity before connecting to the database. At first, a user w ...

  4. MongoDB学习(五)使用Java驱动程序3.3操作MongoDB快速入门

    [引言] 毕竟现在MongoDB还是出于成长阶段,所以现在网上相关的资料很少,而且大部分还都是针对于MongoDB的老版本的.再加上MongoDB的频繁升级.重大更新等等,导致菜鸟学习的难度增大. 好 ...

  5. mongo shell教程

    mongo shell是MongoDB的交互式JavaScript接口,你可以使用mongo shell查询和更新数据以及执行管理操作. 启动mongo shell并连接MongoDB 在启动mong ...

  6. Docker安装MoogoDB, 进入容器, mongo shell操作mongoDB

    安装MoogoDB, 进入容器, mongo shell操作mongoDB [ 包含 Docker-Compose方式.普通方式 ] 文章目录 安装MoogoDB, 进入容器, mongo shell ...

  7. mongo shell连接到mongoDB及shell提示符下执行js脚本

    同mysql数据库类似,mongoDB也可通过mongo客户端连接到mongod服务器来进行绝大多数日常管理.这个命令行工具就是mongo,在mysql中则是mysql.通过mongo命令可以连接到本 ...

  8. java mongodb 关闭连接_如何在mongodb上使用java驱动程序保持连接池关闭?

    我正在从 java驱动程序2.12.3升级到3.3.0.奇怪的是,收集池似乎突然"起作用". 我的设置如下: Connection在主线程中建立: mongoClient = ne ...

  9. MongoDB学习笔记(3)- Mongo Shell 常用查询命令

    MongoDB学习笔记(3)- Mongo Shell 常用查询命令 本文所使用的MongoDB版本为 4.0.10 > db.version(); 4.0.10 一.find 命令进行简查询 ...

最新文章

  1. 基础二维计算几何板子[预备知识]
  2. 第 2 章:初出茅庐【初级篇 - 2.2 贪心算法】
  3. gwt格式_GWT的渐进式Web应用程序配方
  4. Http(s)与后台交互方式
  5. html清除图片缓存
  6. JQuery EasyUI datagrid 键盘上下控制选中行
  7. fifo算法_【算法学习】分枝限界法
  8. Django中加载static无法成功的解决方法
  9. 牛客多校第九场 ZOJ3774 The power of Fibonacci(二次剩余定理+斐波那契数列通项/循环节)题解...
  10. 诺基亚10.22变革影响的分析(转)
  11. web开发网,前端实战项目百度云
  12. 人性的弱点-读书笔记
  13. Cesium中的几种坐标和相互转换
  14. Unity策略游戏集合
  15. Java面试常考之 单例设计模式(饿汉式单例、 懒汉式单例)
  16. Qt嵌入式开发的初步认识
  17. 牛客网——求最小公倍数
  18. 力扣 面试题 17.09. 第 k 个数
  19. 网易云音乐小程序,带后台(SpringBoot)
  20. 1167 susan的货币兑换

热门文章

  1. document.addEventListener的使用介绍
  2. Web页面打印及GridView导出到Excel
  3. TD8.0管理员工具
  4. [转载] 为什么this()和super()必须是构造函数中的第一条语句?
  5. mysql cmmand not found
  6. Java8-2-Lambda表达式实战-一句话实现Map中按照Value排序
  7. delphi构造析构调用顺序
  8. 关于css3的calc()
  9. jquery中html()、text()、val()的区别与使用
  10. sublime text的插件emmet的功能介绍页