Java操作MongoDB 3.x

一. 添加依赖

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.2</version>
</dependency>

二. 创建连接

private static MongoCollection<Document> collection = null;

public static void main(String[] args){
    try {
        MongoClient client = new MongoClient("ip", 27017);
        MongoDatabase mydb = client.getDatabase("mydb");
        System.out.println("连接成功");
        //新创建一个集合
        //mydb.createCollection("userinfo");
        //获取指定集合的连接
        collection = mydb.getCollection("userinfo");
    } catch (Exception e){
        e.printStackTrace();
    }
}

三. 插入

    public void insert(){
        Document document = new Document();
        document.append("name", "Jack");
        document.append("age", 20);
        document.append("scholl", "Beida");
        document.append("address", "guangzhou");
        //插入数组
        document.append("likes", new ArrayList<String>(Arrays.asList(new String[]{"basketball", "football"})));
        //插入内嵌文档
        Document innerDoc = new Document();
        innerDoc.append("father", "Ben");
        innerDoc.append("mother", "meihua");
        document.append("parent", innerDoc);
        //执行插入
        collection.insertOne(document);
    }

四. 查询

1. 查询一个

    /**
     * 查询一个
     */
    public void findOne(){
        //查询第一个文档
        Document document = collection.find().first();
        System.out.println(document.toJson());
    }

2. 查询所有

/**
     * 查询所有
     */
    public void findAll(){
        MongoCursor<Document> cursor = collection.find().iterator();
        while (cursor.hasNext()){
            System.out.println(cursor.next().toJson());
        }
    }

3. 条件查询

    /**
     * 条件查询,使用BasicDBObject
     */
    public void find(){
        BasicDBObject object = new BasicDBObject();
        object.put("name", "Jack");
        Document doc = collection.find(object).first();
        if(doc != null){
            System.out.println(doc.toJson());
        }
    }

五、更新

    /**
     * 更新文档
     */
    public void update(){
        //查询要更新的doc
        BasicDBObject object = new BasicDBObject();
        object.put("age", 20);
        //创建更新的doc
        BasicDBObject newObj = new BasicDBObject();
        newObj.put("age", 50);
        //具体用什么操作更新
        BasicDBObject update = new BasicDBObject("$set", newObj);
        collection.updateOne(object, update);
    }

六、删除

public void delete(){
    BasicDBObject object = new BasicDBObject();
    object.put("name", "Jack");
    collection.deleteOne(object);
}

官方3.0指导:http://mongodb.github.io/mongo-java-driver/3.3/driver/getting-started/quick-tour/
翻译:http://blog.csdn.net/qq_16313365/article/details/52335923
MongoDB Java-API:http://mongodb.github.io/mongo-java-driver/3.4/javadoc/
MongoDB入门:http://wiki.jikexueyuan.com/project/mongodb/mongodb-create-database.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值