标签:ice odata javase tps etc lang 文档 输出 net
转载,原文连接: http://blog.csdn.net/autfish/article/details/51356537
MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别。例如用Document替换BasicDBObject、通过Builders类构建Bson替代直接输入$命令等,本文整理了基于3.2版本的常用增删改查操作的使用方法。为了避免冗长的篇幅,分为增删改、查询、聚合、地理索引等几部分。
创建一个maven项目,添加依赖
- <dependencies>
- <dependency>
- <groupId>org.mongodb</groupId>
- <artifactId>mongodb-driver</artifactId>
- <version>3.2.2</version>
- </dependency>
- </dependencies>
非maven项目可自行下载jar包
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/3.2.2/mongo-java-driver-3.2.2.jar
创建基础代码测试连接
- public class CudExamples {
-
- public static void main(String[] args) throws ParseException {
-
- MongoClient mongoClient = new MongoClient("localhost", 27017);
- MongoDatabase database = mongoClient.getDatabase("lesson");
-
- MongoCollection<Document> mc = database.getCollection("language");
-
- mc.insertOne(new Document("oop", "java"));
- System.out.println(mc.findOneAndDelete(new Document("oop", "java")));
-
- mongoClient.close();
- }
- }
如果输出结果为Document{{_id=573099877bee0e1710d52f4b, oop=java}}表示环境配置正确。
改造CudExamples类以方便演示每种操作对集合的影响
- public class CudExamples {
-
- public static void main(String[] args) throws ParseException {
- MongoClient mongoClient = new MongoClient("localhost", 27017);
- MongoDatabase database = mongoClient.getDatabase("lesson");
-
- CudExamples client = new CudExamples(database);
- client.show();
- mongoClient.close();
- }
-
- private MongoDatabase database;
- public CudExamples(MongoDatabase database) {
- this.database = database;
- }
-
- public void show() {
- MongoCollection<Document> mc = database.getCollection("language");
-
- mc.drop();
-
- mc.insertOne(new Document("oop", "java"));
- printCollection("insert java", mc);
- }
-
-
- public void printCollection(String doing, MongoCollection<Document> mc) {
- System.out.println(doing);
- FindIterable<Document> iterable = mc.find();
- iterable.forEach(new Block<Document>() {
- public void apply(final Document document) {
- System.out.println(document);
- }
- });
- System.out.println("------------------------------------------------------");
- System.out.println();
- }
- }
如上面代码所示,把所有的操作集中在show()方法中演示,并且在执行后打印集合以观察对集合的影响。下面来填充show()方法,注意需要静态导入Updates.*
import static com.mongodb.client.model.Updates.*;
- mc.insertOne(new Document("oop", "java"));
- printCollection("insert java", mc);
-
- Document doc = new Document("oop", "csharp").append("copyright", "microsoft");
- mc.insertOne(doc);
- printCollection("insert csharp", mc);
-
- mc.findOneAndReplace(new Document("oop", "java"), new Document("oop", "java").append("copyright", "oracle"));
- printCollection("findAndReplace java", mc);
-
- mc.deleteOne(new Document("oop", "java"));
- printCollection("delete java", mc);
-
- mc.deleteMany(new Document());
- printCollection("delete all", mc);
-
- mc.insertOne(new Document("oop", "java").append("copyright", "oracle"));
- mc.insertOne(new Document("oop", "csharp").append("copyright", "microsoft"));
- printCollection("insert java,csharp and swift", mc);
-
- mc.updateMany(new Document(), set("rank", 100));
- printCollection("$set all rank 100", mc);
-
- mc.updateOne(new Document("oop", "csharp"), unset("rank"));
- printCollection("unset csharp rank", mc);
-
- mc.updateOne(new Document("oop", "csharp"), inc("rank", 30));
- printCollection("$inc csharp rank 30", mc);
- mc.updateOne(new Document("oop", "csharp"), inc("rank", 31));
- printCollection("$inc csharp rank 31", mc);
-
- mc.updateOne(new Document("oop", "swift").append("copyright", "apple"), setOnInsert("rank", 100), new UpdateOptions().upsert(true));
- printCollection("$setOnInsert rank 100 for swift", mc);
-
- mc.updateOne(new Document("oop", "java"), mul("rank", 0.2));
- printCollection("$mul java rank: 0.2", mc);
-
- mc.updateMany(new Document(), rename("rank", "ranks"));
- printCollection("$rename all rank to ranks", mc);
-
- mc.updateMany(new Document(), min("ranks", 50));
- printCollection("$min all ranks: 50", mc);
-
- mc.updateMany(new Document(), max("ranks", 40));
- printCollection("$max all ranks: 40", mc);
-
- mc.updateMany(new Document("oop", "java"), currentDate("add"));
- printCollection("$currentDate java", mc);
-
- mc.updateMany(new Document("oop", "java"), currentTimestamp("lastModified"));
- printCollection("$currentTimestamp java", mc);
-
- mc.updateMany(new Document("oop", "java"), addToSet("keywords", "for"));
- mc.updateMany(new Document("oop", "java"), addToSet("keywords", "for"));
- printCollection("$addToSet java keywords: for", mc);
-
- mc.updateMany(new Document("oop", "java"), addEachToSet("keywords", Arrays.asList("while", "true", "do", "new", "override")));
- mc.updateMany(new Document("oop", "java"), addEachToSet("keywords", Arrays.asList("while", "true", "do", "new", "override")));
- printCollection("$addEachToSet java keywords: while,true,do,new,override", mc);
-
- mc.updateMany(new Document("oop", "java"), popFirst("keywords"));
- printCollection("$popFirst java keywords", mc);
-
- mc.updateMany(new Document("oop", "java"), popLast("keywords"));
- printCollection("$popLast java keywords", mc);
-
- mc.updateMany(new Document("oop", "java"), pull("keywords", "new"));
- printCollection("$pull java keywords: new", mc);
-
- mc.updateMany(new Document("oop", "java"), pullByFilter(Filters.gte("keywords", "true")));
- printCollection("$pullByFilter java keywords: true", mc);
-
- mc.updateMany(new Document("oop", "java"), pullAll("keywords", Arrays.asList("while", "true", "do", "new", "override")));
- printCollection("$pullAll java keywords", mc);
-
- mc.updateMany(new Document("oop", "java"), push("scores", 89));
- printCollection("$push java scores: 89", mc);
-
- mc.updateMany(new Document("oop", "java"), pushEach("scores", Arrays.asList(89, 90, 92)));
- printCollection("$pushEach java scores: 89,90,92", mc);
-
- mc.updateMany(new Document("oop", "java"), pushEach("scores", Arrays.asList(11, 12, 13), new PushOptions().position(0)));
- printCollection("$pushEach java scores: 11,12,13 at position 0", mc);
-
- mc.updateMany(new Document("oop", "java"), pushEach("scores", Arrays.asList(40, 41), new PushOptions().sort(-1)));
- printCollection("$pushEach java scores: 40,41 and sort(-1)", mc);
-
- mc.updateMany(new Document("oop", "java"), pushEach("scores", Arrays.asList(60, 61), new PushOptions().sort(-1).slice(3)));
- printCollection("$pushEach java scores: 60,61 and sort(-1) and slice(3)", mc);
-
- Bson bson = pushEach("experts",
- Arrays.asList(new Document("first", "Rod").append("last", "Johnson"),
- new Document("first", "Doug").append("last", "Cutting")));
- mc.updateOne(new Document("oop", "java"), bson);
- printCollection("$pushEach", mc);
-
- bson = combine(set("author", "James Gosling"), set("version", "8.0"));
- mc.updateOne(new Document("oop", "java"), bson);
- printCollection("$combine", mc);
(完)
MongoDB-JAVA-Driver 3.2版本常用代码全整理(1) - 增删改
标签:ice odata javase tps etc lang 文档 输出 net
原文地址:http://www.cnblogs.com/sa-dan/p/6836437.html