MongoDB Java Driver 3.4操作

  1. 导入jar包

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.4.2</version>
    </dependency>
    
  2. 连接MongoDB

    实例化一个没有任何连接参数的MongoClient对象,可以连接到运行在本地主机的27017端口

    MongoClient mongoClient = new MongoClient();
    

    我们也可以连接到指定主机的27017端口

    MongoClient mongoClient = new MongoClient("localhost");
    

    指定主机和端口

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    

    指定MongoClientURI

    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
    
  3. 获得数据库和集合

    连接到test库

    MongoDatabase database = mongoClient.getDatabase("test");
    

    获得users集合

    MongoCollection<Document> collection = database.getCollection("users");
    
  4. 查询文档

    用来打印查询结果

    Block<Document> printBlock = new Block<Document>() {
        @Override
        public void apply(Document document) {
            System.out.println(document.toJson());
        }
    };
    

    查询所有文档

    collection.find().forEach(printBlock);
    
    collection.find(new Document()).forEach(printBlock);
    

    复合查询

    collection.find(eq("name", "sue")).forEach(printBlock); // 通过过滤器查询
    
    collection.find(and(gte("age", 20), lt("age", 30), eq("type", 2))).forEach(printBlock); // 通过Filters 过滤
    
    collection.find(new Document("age", new Document("$gte", 20).append("$lt", 30)).append("type", 2)).forEach(printBlock); // 通过 filter document 过滤
    
    

    返回指定的查询字段

    collection.find().projection(new Document("name", 1).append("age", 1).append("type", 1).append("_id", 0)).forEach(printBlock); // 通过.projection()指定返回的字段
    
    collection.find().projection(fields(include("name", "age", "type"), excludeId())).forEach(printBlock);  // 通过Projections类过滤
    
    

    排序

    collection.find().sort(Sorts.ascending("name")).forEach(printBlock);    // 通过.sort() Sorts类排序
    
    
  5. 增加文档

    插入一条数据

    Document document = new Document("name", "webb")
                .append("age", 24)
                .append("type", 1)
                .append("status", "A")
                .append("favorites", new Document("sports", "run").append("food", "photo"));
    
    collection.insertOne(document);   // 插入一条数据
        
    

    插入多条数据

    Document document2 = new Document("name", "lebo")
                .append("age", 24)
                .append("type", 1)
                .append("status", "A")
                .append("favorites", new Document("sports", "run").append("food", "photo"));
    
    List<Document> documents = new ArrayList<>();
    documents.add(document);
    documents.add(document2);
    collection.insertMany(documents); // 插入多条数据
    
    
  6. 更新文档

    修改单个文档

    collection.updateOne(eq("name", "abc"), combine(set("age", 24), set("status", "B")));
    

    修改多个文档

    collection.updateMany(eq("name", "webb"), combine(set("age", 23), set("type", 2)));
    

    如果upsert为true的话,没有匹配到查询条件,就会插入一条。

    collection.updateOne(eq("name", "wenboxu"), combine(set("age", 24)), new UpdateOptions().upsert(true));
    

    替换一条记录

    collection.replaceOne(eq("name", "wenboxu"), new Document("age", "22").append("name", "wenboxu"));
    
  7. 删除文档

    删除单个文档

    collection.deleteOne(eq("name", "webb"));
    

    删除多个文档

    collection.deleteMany(eq("age", 22));
    

    删除所有文档

    collection.deleteMany(new Document());
    
posted @ 2017-05-05 14:04  土豆蛋炒饭  阅读(1628)  评论(0编辑  收藏  举报