文档菜单
文档首页
/ / /
Java 同步驱动程序

快速参考

本页显示了几个 MongoDB 命令的驱动程序语法及其相关参考和 API 文档的链接。

命令
语法
查找文档

API 文档
用法示例
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, ... },
...
]
更新文档中的数组

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))));
迭代地从游标访问数据

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', ... },
...
]
将查询结果作为数组访问

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
列出不同的文档或字段值
coll.distinct("year", Integer.class);
[ 1891, 1893, 1894, 1896, 1903, ... ]
限制检索的文档数量

coll.find().limit(2);
[
{ title: 'My Neighbor Totoro', ... },
{ title: 'Amélie', ... }
]
跳过检索的文档

coll.find(Filters.regex("title", "^Rocky")).skip(2);
[
{ title: 'Rocky III', ... },
{ title: 'Rocky IV', ... },
{ title: 'Rocky V', ... }
]
在检索时对文档进行排序

coll.find().sort(Sorts.ascending("year"));
[
{ title: 'Newark Athlete', year: 1891, ... },
{ title: 'Blacksmith Scene', year: 1893, ...},
{ title: 'Dickson Experimental Sound Film', year: 1894},
...
]
在检索时投影文档字段

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', ... }
]
使用 Maven 安装驱动程序依赖项
pom.xml
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>
使用 Gradle 安装驱动程序依赖项
build.gradle
dependencies {
implementation 'org.mongodb:mongodb-driver-sync:5.2.1'
}

返回

快速入门