使用Mongo Shell和Java驱动程序检查MongoDB文档是否存在的示例

时间:2020-02-23 14:40:49  来源:igfitidea点击:

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

语法是

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

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

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

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

Exists运算符设置为true

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

>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 }

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

将Exists运算符设置为true,并指定判断标准

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

>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 }

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

Exists运算符设置为false

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

>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 }

查询具有空值的文档

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

>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 }

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

适用于现有操作的MongoDB Java程序

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

package com.theitroad.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 exists
	public static void existstrue() throws UnknownHostException {

		//Get a db connection
		MongoClient m1 = new MongoClient("localhost");

		//connect to test db,use your own here
		DB db = m1.getDB("test");

		//obtain the car collection
		DBCollection coll = db.getCollection("car");

		//checking whether regno field exists by setting exists to true
		DBObject query = new BasicDBObject("regno", new BasicDBObject(
				"$exists", true));

		//store the documents in cursor car
		DBCursor car = coll.find(query);

		//iterate and print the contents of cursor
		try {
			while (car.hasNext()) {
				System.out.println(car.next());
			}
		} finally {
			//close the cursor
			car.close();
		}

	}

	//method to check the fields along along with the criteria entered
	public 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 80
		DBObject 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 exist
	public 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 false
		DBObject 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 values
		DBObject 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 methods
		existstrue();
		existstruewithcriteria();
		existsfalse();
		existstruewithnullvalues();
	}

}

上面程序的输出是:

{ "_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的存在是一个易于理解的操作,它可以帮助我们仅选择具有给定字段的特定文档。