MongoDB常用Java|MongoDB常用Java Api
1.连接到单个mongodb实例
(1)连接到端口上localhost上运行的MongoDB实例
MongoClient mongoClient = MongoClients.create();
(2)指定主机名以连接到端口上指定主机上运行的MongoDB实例
MongoClient mongoClient = MongoClients.create((3)指定主机名和端口
MongoClientSettings.builder()
.applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("hostOne")))).build());
MongoClient mongoClient = MongoClients.create((4)指定ConnectionString
MongoClientSettings.builder()
.applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress("hostOne", 27018)))).build());
MongoClient mongClient = MongClients.create("mongodb://hostOne:27017,hostTwo:27018");3.获取数据库对象
MongoDatabase database = mongoClient.getDatabase(dataBaseName);4.获取集合对象
MongoCollection【MongoDB常用Java|MongoDB常用Java Api】5.创建文档对象collection = database.getCollection(collectionName);
{
"name" : "MongoDB",
"type" : "database",
"count" : 1,
"versions": [ "v3.2", "v3.0", "v2.6" ],
"info" : { x : 203, y : 102 }
}
Document document = new Document("name", "MongoDB")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3,2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y", 102));
collection.insertOne(document); --插入单个文档对象
collection.insertMany(documents); --插入多个文档对象6.计算集合中的文档数
collection.countDocuments();7.查询文档对象
(1)查询一个或者第一个文档对象
Document doc = collection.find().first();(2)查询所有文档对象
MongoCursor(3)通过过滤器(Filters)查询文档对象cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
System.out.println(cursor.next().toJson());
}
} finally {
cursor.close();
}
collection.find(eq("name", "张三"));8.更新文档对象
BlockprintBlock = new Block () {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
collection.find(gt("age", 12)).forEach(printBlock);
collection.find(and(gt("age", 12), lte("age", 30))).forEach(printBlock);
collection.updateOne(eq("age", 10), new Document("$set", new Document("age", 100))); --更新单个文档对象
UpdateResult updateResult = collection.updateMany(gt("age", 10), inc("age", 1)); --更新多个文档对象9.删除文档对象
collection.deleteOne(gte("age", 10)).--删除单个文档对象
DeleteResult deleteResult = collection.deleteMany(gte("age", 10)); --删除多个文档对象10.其他查询
(1) 根据sex分组,获取age最大值
Document groupDocument = new Document();(2) 根据sex分组,根据age求和
groupDocument.put("_id", "$sex");
groupDocument.put("max_age",new Document("$max","$age"));
Document group = new Document("$group",groupDocument);
documents.add(group);
MongoCursorcursor = collection.aggregate(documents).iterator();
Document groupDocument = new Document();(3) 查询sex 为FeMale按age降序,并取第11-20条数据
groupDocument.put("_id", "$sex");
groupDocument.put("sum_age",new Document("$sum","$age"));
Document group = new Document("$group",groupDocument);
documents.add(group);
MongoCursorcursor = collection.aggregate(documents).iterator();
MongoCursor(4) 去除sex字段的重复值cursor = collection.find(new Document("sex", "FeMale"))
.sort(new Document("age", -1)).skip(10).limit(10).iterator();
MongoCursor cursor = collection.distinct("sex", String.class).iterator();
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)