java连接mongo数据库

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

package com.hiekn;

import com.mongodb.BasicDBObject;
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 org.bson.Document;

import java.util.ArrayList;
import java.util.HashMap;

public class MongoTest {

    public static void main(String[] args) {


        MongoClient mongoClient = new MongoClient("192.168.20.70", 19130);

        MongoDatabase dbTest = mongoClient.getDatabase("test");
        MongoCollection<Document> collection = dbTest.getCollection("test_collection");
        if(collection == null){
            dbTest.createCollection("test_collection");
        }

        //TODO 1:增加单条数据
        Document doc = new Document();
        doc.append("Name","name1");
        doc.append("Age",20);
        collection.insertOne(doc);

        //TODO 2:增加多条数据
        Document doc2 = new Document();
        doc2.append("Name","name2");
        doc2.append("Age",30);
        ArrayList dataList = new ArrayList();
        dataList.add(doc);
        dataList.add(doc2);
        collection.insertMany(dataList);

        //TODO 3:删除数据
        BasicDBObject delSql = new BasicDBObject("Name", "name1");
        collection.findOneAndDelete(delSql); //删除一条数据
        collection.deleteMany(delSql);//删除多条数据

        //TODO 4:修改数据
        //更新一条数据
        BasicDBObject updatOldSql = new BasicDBObject("Name", "name2");
        BasicDBObject updateNewOneSql = new BasicDBObject("$set", new BasicDBObject("Name", "name1"));
        collection.updateOne(updatOldSql,updateNewOneSql);
        //更新多条数据  修改多个字段
        BasicDBObject updateNewManySql = new BasicDBObject("$set", new BasicDBObject("Name", "name1").append("Age", 66));
        collection.updateMany(updatOldSql,updateNewManySql);

        //TODO 5:查询数据
        BasicDBObject querySql = new BasicDBObject("Name", "name1");
        FindIterable<Document> queryRst = collection.find(querySql);
        MongoCursor<Document> cursor = queryRst.iterator();
        while (cursor.hasNext()){
            System.out.println(cursor.next());
        }

        //TODO 6:高级查询
        BasicDBObject query3 = new BasicDBObject();
        query3.put("name","Tom");
        //查询两个时间范围,用map包装一下
        HashMap<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("$gt","1496911821071");
        queryMap.put("$lt","1496915447871");
        query3.put("timeStamp",new BasicDBObject(queryMap));
        //模糊匹配rule查询
        query3.put("businessRuleName",new BasicDBObject("$regex","rule"));
        FindIterable<Document> result = collection.find(query3);


    }
}

























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值