Docs Menu
Docs Home
/ / /
Java Sync Driver

Quick Reference

This page shows the driver syntax for several MongoDB commands and links to their related reference and API documentation.

Command
Syntax
Find a Document

API Documentation
Usage Example
coll.find(Filters.eq("title", "Hamlet")).first();
{ title: 'Hamlet', type: 'movie', ... }
coll.find(Filters.eq("year", 2005))
[
{ title: 'Christmas in Boston', year: 2005, ... },
{ title: 'Chicken Little', year: 2005, ... },
...
]
coll.insertOne(new Document("title", "Jackie Robinson"));
coll.insertMany(
Arrays.asList(
new Document("title", "Dangal").append("rating", "Not Rated"),
new Document("title", "The Boss Baby").append("rating", "PG")));
coll.updateOne(
Filters.eq("title", "Amadeus"),
Updates.set("imdb.rating", 9.5));
{ title: 'Amadeus', imdb: { rating: 9.5, ... } }
coll.updateMany(
Filters.eq("year", 2001),
Updates.inc("imdb.votes", 100));
[
{ title: 'A Beautiful Mind', year: 2001, imdb: { votes: 826257, ... },
{ title: 'Shaolin Soccer', year: 2001, imdb: { votes: 65442, ... },
...
]
Update an Array in a Document

coll.updateOne(
Filters.eq("title", "Cosmos"),
Updates.push("genres", "Educational"));
{ title: 'Cosmos', genres: [ 'Documentary', 'Educational' ], ...}
coll.replaceOne(
Filters.and(Filters.eq("name", "Deli Llama"), Filters.eq("address", "2 Nassau St")),
new Document("name", "Lord of the Wings").append("zipcode", 10001));
{ name: 'Lord of the Wings', zipcode: 10001 }
coll.deleteOne(Filters.eq("title", "Congo"));
coll.deleteMany(Filters.regex("title", "^Shark.*"));
coll.bulkWrite(
Arrays.asList(
new InsertOneModel<Document>(
new Document().append("title", "A New Movie").append("year", 2022)),
new DeleteManyModel<Document>(
Filters.lt("year", 1970))));
coll.watch(Arrays.asList(
Aggregates.match(Filters.gte("year", 2022))));
Access Data from a Cursor Iteratively

MongoCursor<Document> cursor = coll.find().cursor();
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
[
{ title: '2001: A Space Odyssey', ... },
{ title: 'The Sound of Music', ... },
...
]
Access Results from a Query as an Array

List<Document> resultList = new ArrayList<Document>();
coll.find().into(resultList);
[
{ title: '2001: A Space Odyssey', ... },
{ title: 'The Sound of Music', ... },
...
]
coll.countDocuments(Filters.eq("year", 2000));
618
List the Distinct Documents or Field Values
coll.distinct("year", Integer.class);
[ 1891, 1893, 1894, 1896, 1903, ... ]
Limit the Number of Documents Retrieved

coll.find().limit(2);
[
{ title: 'My Neighbor Totoro', ... },
{ title: 'Amélie', ... }
]
Skip Retrieved Documents

coll.find(Filters.regex("title", "^Rocky")).skip(2);
[
{ title: 'Rocky III', ... },
{ title: 'Rocky IV', ... },
{ title: 'Rocky V', ... }
]
Sort the Documents When Retrieving Them

coll.find().sort(Sorts.ascending("year"));
[
{ title: 'Newark Athlete', year: 1891, ... },
{ title: 'Blacksmith Scene', year: 1893, ...},
{ title: 'Dickson Experimental Sound Film', year: 1894},
...
]
Project Document Fields When Retrieving Them

coll.find().projection(Projections.fields(
Projections.excludeId(),
Projections.include("year", "imdb")));
[
{ year: 2012, imdb: { rating: 5.8, votes: 230, id: 8256 }},
{ year: 1985, imdb: { rating: 7.0, votes: 447, id: 1654 }},
...
]
coll.createIndex(
Indexes.compoundIndex(
Indexes.ascending("title"),
Indexes.descending("year")));
// only searches fields with text indexes
coll.find(Filters.text("zissou"));
[
{ title: 'The Life Aquatic with Steve Zissou', ... }
]
Install the Driver Dependency with Maven
pom.xml
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
Install the Driver Dependency with Gradle
build.gradle
dependencies {
implementation 'org.mongodb:mongodb-driver-sync:5.2.0'
}

Back

Quick Start