Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

MongoDB findOne Example

MongoDB findOne() method returns only one document that satisfies the criteria entered. If the criteria entered matches for more than one document, the method returns only one document according to natural ordering, which reflects the order in which the documents are stored in the database.

MongoDB findOne Syntax

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. The projection parameter accepts the boolean values of 1 or true, 0 or false. If the projection fields are not specified, all the fields will be retrieved.
  2. MongoDB findOne() always includes the _id field even if not specified explicitly in the projection parameter unless it is excluded.
  3. MongoDB findOne() returns only a document but not a cursor.

MongoDB findOne – Empty Query Specification

This operation returns a single document from the collection specified. For example, Output:

"_id" : 2,
"name" : "Polo", "color" : "White",
"cno" : "H411", "speed" : 45, "mfdcountry" : "Japan"

Only one record is retrieved from the car collection. Note that “Polo” was inserted first into the database.

MongoDB findOne – Query Specification

This MongoDB findOne operation returns the first matching document from the specified collection along with the selection criteria entered. For example:

... {
... $or:[
... {name:"Zen"},
... {speed: {$gt:60}} ... ]
... }
... )

"_id" : ObjectId("546cb92393f464ed49d620db"), 
"name" : "Zen",
"color" : "JetRed",
"cno" : "H671",
"speed" : 67, 
"mfdcountry" : "Rome"

This operation searches for the car named “Zen” or for speed greater than 60 and retrieves the first document satisfying the criteria entered from the car collection.

Projection in MongoDB findOne()

The projection parameter is also applicable for MongoDB findOne method. Let’s look at some scenarios where we can use projection in findOne.

Specify Fields to be Returned

This operation displays only the fields specified in the query. For example:

... { },
... {name:1,color:1}
... )

{ "_id" : 2, "name" : "Polo", "color" : "White" }

The first document from the Car collection with id, name, and color fields are displayed.

Return All Fields Except the Excluded One

This operation retrieves the first document excluding the fields specified in the selection criteria. For example;

... { name:"Volkswagen" },
... {_id:0, mfdcountry:0,cno:0 }
... )

{ "name" : "Volkswagen", "color" : "JetBlue", "speed" : 62 }

The car name with Volkswagen is retrieved excluding the id, mfdcountry, and cno fields.

MongoDB findOne Result Document

Cursor methods will not work in this operation as the method returns only a single document. In order to print the individual field values from the document, we can use the below code.

>var car =; 
> if (car) {
...  var carName =;
...  print (tojson(carName));
... }

This retrieves only the name of the car “Polo”.

MongoDB findOne Java Example

Below is the java program showing different options that we can use with MongoDB findOne().

package com.journaldev.mongodb;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;


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

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

	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);



	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);

	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
	public static void main(String[] args) throws UnknownHostException {
		// invoking all the methods from main


Output of above program is:

{ "_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}

That’s all for MongoDB findOne() method, we will look into more MongoDB options in coming posts.

Start Your Cloud Journey Today with Our Free Trial!

Dive into the world of cloud computing with our exclusive free trial offer. Experience the power, flexibility, and scalability of our cloud solutions firsthand.

Try for free!