首页 > 编程语言 > 详细

3.1Mongo Java crud

时间:2021-01-15 13:21:48      阅读:47      评论:0      收藏:0      [点我收藏+]
package MongoQuickStart;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

/**
 * @version 版权 Copyright(c)
 * @ClassName:
 * @Descripton:
 * @author: Shing
 * @date: 2020-12-17 18:56
 */
public class Connect {

    private MongoClient mongoClient;

    public Connect() {
        this.mongoClient = new MongoClient("localhost",27017);
    }

    public MongoDatabase getDatabase(String databaseName){
        return mongoClient.getDatabase(databaseName);
    }

    public MongoCollection<Document> getCollection(MongoDatabase mongoDatabase,String collectionName){
        return mongoDatabase.getCollection(collectionName);
    }

    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("localhost",27017);
        MongoDatabase mongoDatabase = mongoClient.getDatabase("tutorial");
        MongoCollection<Document> collection = mongoDatabase.getCollection("users");
        FindIterable findIterable =  collection.find(Filters.eq("favories.cities","tokyo"));
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()){
            System.out.println(mongoCursor.next().toJson());
        }
    }
}
package MongoQuickStart;

import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.junit.Test;

import java.util.Arrays;

/**
 * @version 版权 Copyright(c)
 * @ClassName:
 * @Descripton:
 * @author: Shing
 * @date: 2020-12-18 06:48
 */
public class Mongocrud {

    private Connect connect = new Connect();

    @Test
    public void testQuery(){
        MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
        MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
        FindIterable<Document> findIterable =  collection.find(Filters.eq("favories.cities","tokyo"));
        for(Document document : findIterable){
            System.out.println(document.toJson());
        }

        System.out.println("查询二");

        collection.find(Filters.and(Filters.eq("favories.cities","tokyo"),
                Filters.eq("username","adele"))).forEach(new Block<Document>() {
            @Override
            public void apply(Document document) {
                System.out.println(document.toJson());
            }
        });
    }

    @Test
    public void testInsert(){
        MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
        MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
        collection.insertOne(new Document("username","Shing").append("country","England")
                .append("favories",new Document("cities", Arrays.asList("tokyo","shanghai"))));
    }

    @Test
    public void testUpdate(){
        MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
        MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
        collection.updateOne(Filters.eq("username","adele"),
                new Document("$set",new Document("favories.cities",Arrays.asList("tokyo"))));
    }

}
更多查询还是看官方文档吧。
https://mongodb.github.io/mongo-java-driver/3.12/driver/getting-started/quick-start/




3.1Mongo Java crud

原文:https://www.cnblogs.com/-shing/p/39f320ba776ee1ca74f5f66961784f82.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!