MongoDB findOne 示例
MongoDB findOne() 方法仅返回一个满足输入条件的文档。如果输入的条件与多个文档匹配,则该方法将按照自然排序仅返回一个文档,这反映了文档在数据库中的存储顺序。
MongoDB 查找一个
MongoDB findOne() 语法是:db.collection.findOne(<criteria>, <projection>)
criteria - 指定输入的选择标准。projection - 指定返回文档中要显示的字段列表。关于 MongoDB findOne 的几个要点:
- 投影参数接受布尔值 1 或 true 、 0 或 false 。如果未指定投影字段,则将检索所有字段。
- MongoDB findOne() 始终包含 _id 字段,即使在投影参数中未明确指定,除非将其排除。
- MongoDB findOne() 仅返回文档但不返回游标。
MongoDB findOne - 空查询规范
此操作返回指定集合中的单个文档。例如,db.car.findOne()
输出:
{
"_id" : 2,
"name" : "Polo", "color" : "White",
"cno" : "H411", "speed" : 45, "mfdcountry" : "Japan"
}
从集合中只检索到一条记录car
。请注意,“Polo”首先插入到数据库中。
MongoDB findOne - 查询规范
此 MongoDB findOne 操作将返回指定集合中的第一个匹配文档以及输入的选择条件。例如:
>db.car.findOne(
... {
... $or:[
... {name:"Zen"},
... {speed: {$gt:60}} ... ]
... }
... )
{
"_id" : ObjectId("546cb92393f464ed49d620db"),
"name" : "Zen",
"color" : "JetRed",
"cno" : "H671",
"speed" : 67,
"mfdcountry" : "Rome"
}
此操作搜索名为“Zen”或速度大于 60 的汽车,并从汽车集合中检索满足输入条件的第一个文档。
MongoDB 中的投影 findOne()
投影参数也适用于 MongoDB findOne 方法。让我们看看在 findOne 中可以使用投影的一些场景。
MongoDB findOne-指定要返回的字段
此操作仅显示查询中指定的字段。例如:
>db.car.findOne(
... { },
... {name:1,color:1}
... )
{ "_id" : 2, "name" : "Polo", "color" : "White" }
显示 Car 系列中带有 id、name 和 color 字段的第一个文档。
MongoDB findOne - 返回除排除的字段之外的所有字段
此操作检索第一个不包含选择条件中指定的字段的文档。例如;
>db.car.findOne(
... { name:"Volkswagen" },
... {_id:0, mfdcountry:0,cno:0 }
... )
{ "name" : "Volkswagen", "color" : "JetBlue", "speed" : 62 }
检索到带有 Volkswagen 的汽车名称,不包括 id、mfdcountry 和 cno 字段。
MongoDB findOne 结果文档
游标方法在此操作中不起作用,因为该方法仅返回单个文档。为了打印文档中的各个字段值,我们可以使用以下代码。
>var car = db.car.findOne();
> if (car) {
... var carName = car.name;
... print (tojson(carName));
... }
"Polo"
这仅检索汽车名称“Polo”。
MongoDB findOne Java 示例
下面是 java 程序,展示了我们可以与 MongoDB findOne() 一起使用的不同选项。MongoDBFindOne.java
package com.journaldev.mongodb;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
public class MongoDBFindOne {
// method retrieves the first document without any criteria
public static void emptyFindOne() throws UnknownHostException {
// Get a new connection to the db assuming that it is running
MongoClient mongoClient = new MongoClient("localhost");
// use test as a datbase,use your database here
DB db = mongoClient.getDB("test");
// fetch the collection object ,car is used here,use your own
DBCollection coll = db.getCollection("car");
// invoking findOne() method
DBObject doc = coll.findOne();
// prints the resultant document
System.out.println(doc);
}
// method that retrieves the document based on the selection criteria
public static void querySpecification() throws UnknownHostException {
// getting a connection everytime is not needed (could be done once
// globally).
MongoClient mongoClient = new MongoClient("localhost");
DB db = mongoClient.getDB("test");
DBCollection coll = db.getCollection("car");
// query to filter the document based on name and speed values by
// creating new object
DBObject query = new BasicDBObject("name", "Zen").append("speed",
new BasicDBObject("$gt", 30));
// resultant document fetched by satisfying the criteria
DBObject d1 = coll.findOne(query);
// prints the document on console
System.out.println(d1);
}
public static void projectionFields() throws UnknownHostException {
MongoClient mongoClient = new MongoClient("localhost");
DB db = mongoClient.getDB("test");
DBCollection coll = db.getCollection("car");
// creates new db object
BasicDBObject b1 = new BasicDBObject();
// criteria to display only name and color fields in the resultant
// document
BasicDBObject fields = new BasicDBObject("name", 1).append("color", 1);
// method that retrieves the documents by accepting fields and object
// criteria
DBObject d1 = coll.findOne(b1, fields);
System.out.println(d1);
}
public static void excludeByfields() throws UnknownHostException {
MongoClient m1 = new MongoClient("localhost");
DB db = m1.getDB("test");
DBCollection col = db.getCollection("car");
// filter criteria for car name volkswagen
DBObject query = new BasicDBObject("name", "Volkswagen");
// excluding the fields mfdcountry,cno and id fields
BasicDBObject fields = new BasicDBObject("mfdcountry", 0).append("cno",
0).append("_id", 0);
DBObject d1 = col.findOne(query, fields);
System.out.println(d1);
}
public static void printDoc() throws UnknownHostException {
MongoClient m1 = new MongoClient("localhost");
DB db = m1.getDB("test");
DBCollection col = db.getCollection("car");
DBObject d1 = col.findOne();
// prints only the name of the car
System.out.println((d1.get("name")));
}
public static void main(String[] args) throws UnknownHostException {
// invoking all the methods from main
emptyFindOne();
querySpecification();
projectionFields();
excludeByfields();
printDoc();
}
}
上述程序的输出是:
{ "_id" : 2.0 , "name" : "Polo" , "color" : "White" , "cno" : "H411" , "speed" : 45.0 , "mfdcountry" : "Japan"}
{ "_id" : { "$oid" : "546cb92393f464ed49d620db"} , "name" : "Zen" , "color" : "JetRed" , "cno" : "H671" , "speed" : 67 , "mfdcountry" : "Rome"}
{ "_id" : 2.0 , "name" : "Polo" , "color" : "White"}
{ "name" : "Volkswagen" , "color" : "JetBlue" , "speed" : 62}
Polo
这就是 MongoDB findOne() 方法的全部内容,我们将在后续文章中探讨更多 MongoDB 选项。参考:官方文档