这里的示例代码都是简要代码,详细代码可以参考我打包好的代码,地址:百度云 mongo-demo.rar
Insert
- insertOne() : 插入单个
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 构建Document
Document doc = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("info", new Document("x", 203).append("y", 102));
// 插入单个Document
collection.insertOne(doc);
// 关闭连接
client.close();
语法解释:(如果看不懂解释,那么请参考:MongoDB学习日记(四):insert)
db.test.insert({
name : "MongoDB",
type : "database",
count : 1,
info : {
x : 203,
y : 102
}
})
- insertMany() : 插入多个
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 用List来存放多个Document
List<Document> documents = new ArrayList<>();
for (int i = 0; i < 100; i++) {
documents.add(new Document("i", i));
}
// 插入多个Document
collection.insertMany(documents);
// count()得到集合的数据总数
System.out.println(collection.count());
//关闭连接
client.close();
语法解释:
for(var i; i < 100; i++ ) {
db.test.insert({"i":i});
}
/* count() */
db.test.count()
find
(注:如果看不懂代码中的语法解释,请参考:MongoDB学习日记(五):find)
- find()
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 : db.test.find()
FindIterable<Document> docs = collection.find();
for (Document document : docs) {
System.out.println(document.toJson());
}
- frist()
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 :db.collection.find().limit(1),当然也可以使用javascript取结果集的第一个
Document myDoc = collection.find().first();
System.out.println(myDoc.toJson());
- Filters.gt() / Filters.lt() …
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 :db.collection.find({i:1})
FindIterable<Document> docs = collection.find(Filters.eq("i",1));
for (Document document : docs) {
System.out.println(document.toJson());
}
- Filters.and() / Filters.or() …
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 : db.collection.find({i:{$gt:1},i:{$lt:3}})
FindIterable<Document> docs = collection.find(Filters.and(Filters.gt("i",1),Filters.lt("i",3)));
for (Document document : docs) {
System.out.println(document.toJson());
}
- iterator()
iterator 是 MongoDB 提供的遍历的方法,返回 MongoCursor<Document> cursor
。
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// iterator 遍历
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
- block
注意,block 不是一个方法,而是一个接口对象,可以按照自己的需要,来操作 Document。
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 创建Block对象,这里就打印下查询到的 Document
Block<Document> printBlock = new Block<Document>() {
@Override
public void apply(Document t) {
System.out.println(t.toJson());
}
};
// 查询到的结果,调用forEach遍历执行printBlock
collection.find(Filters.gt("i", 90)).forEach(printBlock);
- sort()
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 : db.collection.find({i:{$exists:true}},{_id:0}).sort({i:-1}).limit(1)
Document myDoc = collection.find(Filters.exists("i")).sort(Sorts.descending("i")).first();
System.out.println(myDoc.toJson());
注意 : exists()
和 $exists
,$exists
之前也讲过了,个人感觉这个应该很有用的,因为 MongoDB 的存储是没有固定字段的。
- Projections.excludeId() :不查询_id
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 : db.collection.find({},{_id:0})
Document myDoc = collection.find().projection(Projections.excludeId()).first();
System.out.println(myDoc.toJson());
update
(注:如果看不懂此部分代码中的语法解释,请参考:MongoDB学习日记(六):update)
- updateOne()
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 : db.test.update({i:10}},{$set:{i:110}})
UpdateResult ur = collection.updateOne(Filters.eq("i", 1), new Document("$set", new Document("i", 110)));
System.out.println(ur);
FindIterable<Document> docs = collection.find(Filters.eq("i", 110));
for (Document document : docs) {
System.out.println(document.toJson());
}
client.close();
- updateMany()
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
FindIterable<Document> docs = collection.find(Filters.eq("i",110));
for (Document document : docs) {
System.out.println(document.toJson());
}
// 语法解释 : db.test.update({i:{$lt:100}},{$inc:{i:100}})
UpdateResult updateResult = collection.updateMany(Filters.lt("i", 100),
new Document("$inc", new Document("i", 100)));
System.out.println(updateResult.getModifiedCount());
// 查询检验
FindIterable<Document> documents = collection.find();
for (Document document : documents) {
System.out.println(document.toJson());
}
client.close();
replaceOne() :替换
(注:只有 replaceOne,没有 replaceMany)
// 建立连接
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
// 语法解释 :先find查询,然后使用整体更新:db.test.update({i:120},document)
UpdateResult ur = collection.replaceOne(Filters.eq("i", 120), new Document("i", 200));
System.out.println(ur);
FindIterable<Document> docs = collection.find(Filters.eq("i", 200));
for (Document document : docs) {
System.out.println(document.toJson());
}
client.close();
delete
- deleteOne()
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
DeleteResult dr = collection.deleteOne(Filters.eq("i", 100));
System.out.println(dr);
client.close();
- deleteMany()
MongoClient client = new MongoClient();
MongoDatabase database = client.getDatabase("mydb");
MongoCollection<Document> collection = database.getCollection("test");
DeleteResult dr = collection.deleteMany(Filters.exists("i"));
System.out.println(dr);
client.close();
我写博客时多加了很多解释和代码,可能不小心贴错代码,具体以我分享的代码为主。