mongo-java-driver -3.2.2学习笔记-01-MongoDB Driver Quick Tour
2017-12-05 11:47
579 查看
连接
MongoClient 代表一个mongo的数据库连接池,只需要一个单例进行操作
获得集合
向集合中插入文档
插入多条文档
查询
以过滤条件查询文档
查询一系列的文档,并且对于每个文档进行操作
对文档进行排序
排除查询到的某个文档
聚集查询
更新文档s
删除文档s
批量操作
// To directly connect to a single MongoDB server // (this will not auto-discover the primary even if it's a member of a replica set) MongoClient mongoClient = new MongoClient(); // or MongoClient mongoClient = new MongoClient( "localhost" ); // or MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members MongoClient mongoClient = new MongoClient( Arrays.asList(new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019))); // or use a connection string MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019"); MongoClient mongoClient = new MongoClient(connectionString); MongoDatabase database = mongoClient.getDatabase("mydb");
MongoClient 代表一个mongo的数据库连接池,只需要一个单例进行操作
获得集合
MongoCollection<Document> collection = database.getCollection("test");
向集合中插入文档
Document doc = new Document("name", "MongoDB") .append("type", "database") .append("count", 1) .append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc);
插入多条文档
List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents);
查询
System.out.println(collection.count());//文档计数 Document myDoc = collection.find().first(); System.out.println(myDoc.toJson());//查询文档第一条记录 MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); }//以游标的方式查询所有的文档 for (Document cur : collection.find()) { System.out.println(cur.toJson()); }//查询文档
以过滤条件查询文档
import static com.mongodb.client.model.Filters.*; myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson());
查询一系列的文档,并且对于每个文档进行操作
// now use a range query to get a larger subset Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock);
对文档进行排序
myDoc = collection.find(exists("i")).sort(descending("i")).first(); System.out.println(myDoc.toJson());
排除查询到的某个文档
myDoc = collection.find().projection(excludeId()).first(); System.out.println(myDoc.toJson());
聚集查询
collection.aggregate(asList( match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}"))) ).forEach(printBlock); myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first(); System.out.println(myDoc.toJson());
更新文档s
collection.updateOne(eq("i", 10), set("i", 110)); UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100)); System.out.println(updateResult.getModifiedCount());
删除文档s
collection.deleteOne(eq("i", 110)); DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount());
批量操作
// 2. Ordered bulk operation - order is guarenteed collection.bulkWrite( Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), new InsertOneModel<>(new Document("_id", 5)), new InsertOneModel<>(new Document("_id", 6)), new UpdateOneModel<>(new Document("_id", 1), new Document("$set", new Document("x", 2))), new DeleteOneModel<>(new Document("_id", 2)), new ReplaceOneModel<>(new Document("_id", 3), new Document("_id", 3).append("x", 4)))); // 2. Unordered bulk operation - no guarantee of order of operation collection.bulkWrite( Arrays.asList(new InsertOneModel<>(new Document("_id", 4)), new InsertOneModel<>(new Document("_id", 5)), new InsertOneModel<>(new Document("_id", 6)), new UpdateOneModel<>(new Document("_id", 1), new Document("$set", new Document("x", 2))), new DeleteOneModel<>(new Document("_id", 2)), new ReplaceOneModel<>(new Document("_id", 3), new Document("_id", 3).append("x", 4))), new BulkWriteOptions().ordered(false));
相关文章推荐
- mongo-java-driver -3.2.2学习笔记-02-MongoDB Driver Admin Quick Tour
- mongo-java-driver -3.2.2学习笔记-09-driver-async
- mongo-java-driver -3.2.2学习笔记-04-ssl
- mongo-java-driver -3.2.2学习笔记-07-GridFs
- mongo-java-driver -3.2.2学习笔记-05-身份认证
- mongo-java-driver -3.2.2学习笔记-06-CRUD
- mongo-java-driver -3.2.2学习笔记-08-Monitoring
- mongo-java-driver -3.2.2学习笔记-03-Connecting
- MongoDB入门学习笔记之mongo shell和java客户端
- JAVA学习笔记——面向对象01
- java个人学习笔记01(配置java环境)
- MongoDB java客户端mongo-java-driver使用
- Java学习笔记01---可视化输入输出,数据类型转化,流程控制
- 黑马程序员_java基础学习笔记01_变量,数据类型,运算符
- Java学习笔记 - 01
- Java线程学习笔记----01
- mongodb 学习笔记 01 -- 概述,安装,运行
- 转载:mongoDB java驱动学习笔记
- Java 学习笔记01:使用JDBC连接MYSQL数据库
- [Spring Data MongoDB]学习笔记--注册一个Mongo实例