MongoDB findOne 示例

MongoDB findOne() 方法只返回一个符合输入的 criteria的文档. 如果输入的标准对应于多个文档,该方法只返回一个文档按照自然排序,这反映了文档存储在数据库中的顺序。

MongoDB 找到一

MongoDB findOne MongoDB findOne() syntax is: db.collection.findOne(<criteria>, <projection>) criteria - specifies the selection criteria entered. projection - specifies the list of fields to be displayed in the returned document. Few important points about MongoDB findOne:

  1. 投影参数接受 1 或 true, 0 或 false 的布尔值. 如果投影字段未指定,所有字段将被检索
  2. MongoDB findOne() 总是包含 _id 字段,即使在投影参数中没有明确指定,除非它被排除
  3. MongoDB findOne() 只返回文档,而不是路由器

MongoDB findOne - 空查询规格

此操作返回指定的集合中的单个文档. 例如, db.car.findOne() 输出:

1{
2"_id" : 2,
3"name" : "Polo", "color" : "White",
4"cno" : "H411", "speed" : 45, "mfdcountry" : "Japan"
5}

仅从汽车集合中获取了一个记录,请注意Polo首先被插入数据库。

MongoDB findOne - 查询规格

此 MongoDB findOne 操作返回指定的集合中的第一个匹配文档,以及输入的选择标准。

 1>db.car.findOne(
 2... {
 3... $or:[
 4... {name:"Zen"},
 5... {speed: {$gt:60}} ... ]
 6... }
 7... )
 8
 9{
10"_id" : ObjectId("546cb92393f464ed49d620db"), 
11"name" : "Zen",
12"color" : "JetRed",
13"cno" : "H671",
14"speed" : 67, 
15"mfdcountry" : "Rome"
16}

此操作搜索名为Zen的汽车或超过60的速度,并从汽车收集中获取第一个符合输入的标准的文件。

在 MongoDB findOne() 中投影

投影参数也适用于 MongoDB findOne 方法. 让我们看看我们可以在 findOne 中使用投影的某些场景。

MongoDB findOne - 指定要返回的字段

此操作只显示查询中指定的字段,例如:

1>db.car.findOne(
2... { },
3... {name:1,color:1}
4... )
5
6{ "_id" : 2, "name" : "Polo", "color" : "White" }

显示了来自汽车收藏的第一个文件,其中包含ID、名称和颜色字段。

MongoDB findOne - 返回除排除外的所有字段

此操作会检索第一份文档,不包括选项标准中指定的字段;例如;

1>db.car.findOne(
2... { name:"Volkswagen" },
3... {_id:0, mfdcountry:0,cno:0 }
4... )
5
6{ "name" : "Volkswagen", "color" : "JetBlue", "speed" : 62 }

与 Volkswagen 的汽车名称被检索,不包括 id、 mfdcountry 和 cno 字段。

MongoDB findOne 结果文件

指标方法不会在此操作中工作,因为该方法只返回一个文档. 为了从文档中打印单个字段值,我们可以使用下面的代码。

1>var car = db.car.findOne(); 
2> if (car) {
3...  var carName = car.name;
4...  print (tojson(carName));
5... }
6
7"Polo"

这只恢复了汽车的名称Polo

MongoDB find一个Java例子

下面是 Java 程序,展示了我们可以使用 MongoDB findOne()的不同选项。

  1package com.journaldev.mongodb;
  2
  3import com.mongodb.BasicDBObject;
  4import com.mongodb.DB;
  5import com.mongodb.DBCollection;
  6import com.mongodb.DBObject;
  7import com.mongodb.MongoClient;
  8
  9import java.net.UnknownHostException;
 10
 11public class MongoDBFindOne {
 12
 13    // method retrieves the first document without any criteria
 14    public static void emptyFindOne() throws UnknownHostException {
 15
 16    	// Get a new connection to the db assuming that it is running
 17    	MongoClient mongoClient = new MongoClient("localhost");
 18
 19    	// use test as a datbase,use your database here
 20    	DB db = mongoClient.getDB("test");
 21
 22    	// fetch the collection object ,car is used here,use your own
 23    	DBCollection coll = db.getCollection("car");
 24
 25    	// invoking findOne() method
 26    	DBObject doc = coll.findOne();
 27
 28    	// prints the resultant document
 29    	System.out.println(doc);
 30    }
 31
 32    // method that retrieves the document based on the selection criteria
 33    public static void querySpecification() throws UnknownHostException {
 34    	// getting a connection everytime is not needed (could be done once
 35    	// globally).
 36    	MongoClient mongoClient = new MongoClient("localhost");
 37    	DB db = mongoClient.getDB("test");
 38    	DBCollection coll = db.getCollection("car");
 39
 40    	// query to filter the document based on name and speed values by
 41    	// creating new object
 42    	DBObject query = new BasicDBObject("name", "Zen").append("speed",
 43    			new BasicDBObject("$gt", 30));
 44
 45    	// resultant document fetched by satisfying the criteria
 46    	DBObject d1 = coll.findOne(query);
 47
 48    	// prints the document on console
 49    	System.out.println(d1);
 50    }
 51
 52    public static void projectionFields() throws UnknownHostException {
 53
 54    	MongoClient mongoClient = new MongoClient("localhost");
 55    	DB db = mongoClient.getDB("test");
 56    	DBCollection coll = db.getCollection("car");
 57
 58    	// creates new db object
 59    	BasicDBObject b1 = new BasicDBObject();
 60
 61    	// criteria to display only name and color fields in the resultant
 62    	// document
 63    	BasicDBObject fields = new BasicDBObject("name", 1).append("color", 1);
 64
 65    	// method that retrieves the documents by accepting fields and object
 66    	// criteria
 67    	DBObject d1 = coll.findOne(b1, fields);
 68
 69    	System.out.println(d1);
 70
 71    }
 72
 73    public static void excludeByfields() throws UnknownHostException {
 74    	MongoClient m1 = new MongoClient("localhost");
 75
 76    	DB db = m1.getDB("test");
 77    	DBCollection col = db.getCollection("car");
 78
 79    	// filter criteria for car name volkswagen
 80    	DBObject query = new BasicDBObject("name", "Volkswagen");
 81
 82    	// excluding the fields mfdcountry,cno and id fields
 83    	BasicDBObject fields = new BasicDBObject("mfdcountry", 0).append("cno",
 84    			0).append("_id", 0);
 85
 86    	DBObject d1 = col.findOne(query, fields);
 87    	System.out.println(d1);
 88    }
 89
 90    public static void printDoc() throws UnknownHostException {
 91    	MongoClient m1 = new MongoClient("localhost");
 92    	DB db = m1.getDB("test");
 93    	DBCollection col = db.getCollection("car");
 94    	
 95    	DBObject d1 = col.findOne();
 96    	
 97    	// prints only the name of the car
 98    	System.out.println((d1.get("name")));
 99    }
100    
101    public static void main(String[] args) throws UnknownHostException {
102    	// invoking all the methods from main
103    	emptyFindOne();
104    	querySpecification();
105    	projectionFields();
106    	excludeByfields();
107    	printDoc();
108    }
109
110}

上述方案的结果是:

1{ "_id" : 2.0 , "name" : "Polo" , "color" : "White" , "cno" : "H411" , "speed" : 45.0 , "mfdcountry" : "Japan"} ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
2{ "_id" : { "$oid" : "546cb92393f464ed49d620db"} , "name" : "Zen" , "color" : "JetRed" , "cno" : "H671" , "speed" : 67 , "mfdcountry" : "Rome"}
3{ "_id" : 2.0 , "name" : "Polo" , "color" : "White"}
4{ "name" : "Volkswagen" , "color" : "JetBlue" , "speed" : 62}
5Polo

这就是MongoDB findOne()方法的全部,我们将在即将到来的帖子中查看更多MongoDB选项。

Published At
Categories with 技术
Tagged with
comments powered by Disqus