mongo-java-driver -3.2.2学习笔记-06-CRUD
2017-12-05 12:41
246 查看
mongo-java-driver -3.2.2学习笔记-06-CRUD
example1
example 2:
important!!!!!
mongo 进行不同的读写配置,见官方文档
example1
MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("mydb"); MongoCollection<Document> collection = database.getCollection("mycoll"); // insert a document Document document = new Document("x", 1) collection.insertOne(document); document.append("x", 2).append("y", 3); // replace a document collection.replaceOne(Filters.eq("_id", document.get("_id")), document); // find documents List<Document> foundDocument = collection.find().into(new ArrayList<Document>());
example 2:
// Pass BasicDBObject.class as the second argument MongoCollection<BasicDBObject> collection = database.getCollection("mycoll", BasicDBObject.class); // insert a document BasicDBObject document = new BasicDBObject("x", 1) collection.insertOne(document); document.append("x", 2).append("y", 3); // replace a document collection.replaceOne(Filters.eq("_id", document.get("_id"), document); // find documents List<BasicDBObject> foundDocument = collection.find().into(new ArrayList<BasicDBObject>());
important!!!!!
mongo 进行不同的读写配置,见官方文档
// CORRECT: The results of the method calls are chained and the final one is referenced // by collection MongoCollection<Document> collection = database.getCollection("mycoll") .withWriteConcern(WriteConcern.JOURNALED) .withReadPreference(ReadPreference.primary()) .withCodecRegistry(newRegistry); // INCORRECT: withReadPreference returns a new instance of MongoCollection // It does not modify the collection it's called on. So this will // have no effect collection.withReadPreference(ReadPreference.secondary());
相关文章推荐
- mongo-java-driver -3.2.2学习笔记-07-GridFs
- mongo-java-driver -3.2.2学习笔记-09-driver-async
- mongo-java-driver -3.2.2学习笔记-01-MongoDB Driver Quick Tour
- mongo-java-driver -3.2.2学习笔记-05-身份认证
- mongo-java-driver -3.2.2学习笔记-08-Monitoring
- mongo-java-driver -3.2.2学习笔记-02-MongoDB Driver Admin Quick Tour
- mongo-java-driver -3.2.2学习笔记-04-ssl
- mongo-java-driver -3.2.2学习笔记-03-Connecting
- Java学习笔记 - 06
- JAVA学习笔记17:JDBC Driver
- 【学习笔记06】java面向对象-main方法、单例设计模式
- java学习笔记06--正则表达式
- java个人学习笔记06(类、对象、成员变量、局部变量)
- Java学习笔记06——JDBC
- MongoDB学习笔记:(3)、mongodb 3.2在java环境中的简单CRUD
- (06)Java学习笔记——数组
- thinkinginjava学习笔记06_复用类
- Java学习笔记06 String和基本数据类型
- mongo-java-driver 3.0版本学习例子 根据官网指南写的
- selenium(webdriver)学习笔记6--Alert/Confirm/Prompt的处理(Java版)