使用java操作MongoDB

使用java操作MongoDB

下载mongoDB对Java支持的驱动包
驱动包下载地址:
https://github.com/mongodb/mongo-java-driver/downloads

1、查询集合中所有文档

Mongo mongo = new Mongo("localhost",27017);
DB db = mongo.getDB("test");
DBCollection collection = db.getCollection("customer");
DBCursor dbCursor = collection.find();
System.out.println(collection.getCount());
while(dbCursor.hasNext()){
    System.out.println(dbCursor.next());
}
mongo.close();

2、删除集合中的文档

Mongo mongo = new Mongo("localhost",27017);
DB db = mongo.getDB("test");
DBCollection collection = db.getCollection("customer");
BasicDBObject o = new BasicDBObject("_id", new ObjectId("5199ee647d5fc789bc760c07"));
collection.remove(o);
mongo.close();

3、向集合中插入文档

Mongo mongo = new Mongo("localhost",27017);
DB db = mongo.getDB("test");
DBCollection collection = db.getCollection("customer");
DBObject c = new BasicDBObject();
c.put("name", “jack");
c.put("age", 24);
collection.insert(c);
mongo.close();

4、更新集合中的文档

Mongo mongo = new Mongo("localhost",27017);
DB db = mongo.getDB("test");
DBCollection collection = db.getCollection("customer");
BasicDBObject query = new BasicDBObject("_id",new ObjectId("519e2e393296cf3baccdb10c"));
BasicDBObject object = (BasicDBObject) collection.findOne(query);
object.put("name", “wangwu");
int n = collection.update(query, object).getN();
System.out.println(n);
mongo.close();

代码:

package cn.hcx.dao;

import java.net.UnknownHostException;

import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

/**
 * 使用java 代码调用mongodb 
 * @author HCX
 *
 */
public class MongoDao {
    @Test
    public void add() throws UnknownHostException{
        Mongo mongo=new Mongo("127.0.0.1", 3333);
        
        DB db=mongo.getDB("test");
        
        DBCollection collection=db.getCollection("person");
        //{key:value,key:value}
        BasicDBObject dbObject=new BasicDBObject();
        
        dbObject.put("name", "hcx");
        
        dbObject.put("age", 18);
        
        dbObject.put("sex", "女");
        
        collection.insert(dbObject);
        
        mongo.close();
    }
    @Test
    public void del() throws UnknownHostException{
        Mongo mongo=new Mongo("127.0.0.1", 3333);
        
        DB db=mongo.getDB("test");
        
        DBCollection collection=db.getCollection("person");
        //{key:value}
        
        
        //{$nor:[{name:”user2”},{age:3}]
        //db.collection.find({age:{$gt:10}})
        //{}
        DBObject dbObject=new BasicDBObject();
        //dbObject.put("key",{})  {key:{}}
        
        
        collection.remove(dbObject);
        
        mongo.close();
        
    }
    public void update(){
        
    }
    @Test
    public void query() throws UnknownHostException{
        Mongo mongo=new Mongo("127.0.0.1", 3333);
        
        DB db=mongo.getDB("test");
        
        DBCollection collection=db.getCollection("person");
        
//      BasicDBObject dbObject=new BasicDBObject();
        
//      dbObject.put("name", "hcx");
        DBCursor dbCursor=collection.find();
        
        while(dbCursor.hasNext()){
            System.out.println(dbCursor.next().toString());
        }
        
    }

}
    原文作者:JS_HCX
    原文地址: https://www.jianshu.com/p/14e442e80a55
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞