java对mongodb数据库的增删改查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QQ736238785/article/details/79963788

数据结果图片
这里写图片描述

具体代码

/**
 * @author Administrator
 *实现对mongodb数据库的增删改查
 */
public class mongoDB {
    static MongoClient mongoClient =null;
    static DB db =null;

    public mongoDB(String baseName){
        mongoClient = new MongoClient("localhost",27017);
        db = mongoClient.getDB(baseName);
    }

    public static void main(String args[]){

    mongoDB  mongodb = new mongoDB("testDB");
//    mongodb.createCollect("test", new BasicDBObject());
//    mongodb.insertCollect("test", new BasicDBObject());
//     mongodb.insertBatch("test", new ArrayList<DBObject>());  
//     mongodb.deleteById("test", new ObjectId("5ad46224f46a9b00b8e6da66"));
//       mongodb.deleteByName("test", "wangwu");
      /* Cursor cursor = mongodb.findAll("test");
       while(cursor.hasNext()){
           DBObject obj = cursor.next();
           System.out.println(obj.get("name")+" "+obj.get("age")+" "+obj.get("sex"));
       }*/
    /*Cursor cursor = mongodb.findByName("test","wangwu");
    while(cursor.hasNext()){
       DBObject obj = cursor.next();
       System.out.println(obj.get("name")+" "+obj.get("age")+" "+obj.get("sex"));
     }*/
    /*Cursor cursor = mongodb.findByPage("test", 0, 1);
    while(cursor.hasNext()){
       DBObject obj = cursor.next();
       System.out.println(obj.get("name")+" "+obj.get("age")+" "+obj.get("sex"));
      }*/
    int n = mongodb.updateCollect("test", new BasicDBObject(), new BasicDBObject(), false, true);
    System.out.println("有"+n+"条记录被修改");
    }

    //创建集合
    public void createCollect(String collectName,DBObject object){
        db.createCollection(collectName, object);
        System.out.println("创建集合成功!");
    }

    //插入文档
    public void insertCollect(String collectName,DBObject object){
        object.put("name", "zhangsan");
        object.put("age",23);
        object.put("sex", "boy");
        DBCollection collection = db.getCollection(collectName);
        collection.insert(object);
        System.out.println("插入数据成功!");

    }

    //批量插入
    public void insertBatch(String collectName,ArrayList<DBObject> object){
         DBObject obj1 =new BasicDBObject();
        obj1.put("name", "lisi");
        DBObject obj2 =new BasicDBObject();
        obj2.put("name", "wangwu");
        object.add(obj1);
        object.add(obj2);
       DBCollection collection = db.getCollection(collectName);
       collection.insert(object);
       System.out.println("插入批量数据成功!");
    }

    //根据id删除
    public void deleteById(String collectName,ObjectId id){
          DBCollection collection = db.getCollection(collectName);
          DBObject obj = new BasicDBObject();
          obj.put("_id",id);
          collection.remove(obj);
          System.out.println("根据id删除成功!");
    }

    //根据name删除
    public void deleteByName(String collectName,String name){
          DBCollection collection = db.getCollection(collectName);
          DBObject obj = new BasicDBObject();
          obj.put("name",name);
          collection.remove(obj);
          System.out.println("根据名字删除成功!");
    }

    //查询所有
    public Cursor findAll(String collectName){
        Cursor cursor = null;
        DBCollection collection = db.getCollection(collectName);
        cursor = collection.find();
        return cursor;
    }

    //根据名字查询
    public Cursor findByName(String collectName,String name){
        Cursor cursor = null;
        DBCollection collection = db.getCollection(collectName);
        DBObject obj = new BasicDBObject();
        obj.put("name",name);
        cursor = collection.find(obj);
        return cursor;
    }

    //分页查询
    public Cursor findByPage(String collectName,int start,int limits){
        Cursor cursor = null;
        DBCollection collection = db.getCollection(collectName);
        cursor = collection.find().limit(limits).skip(start);
        return cursor;
    }

    //修改
    public int updateCollect(String collectName,DBObject query,DBObject update,boolean upsert,boolean multi){
        int count =0;
        DBCollection collection = db.getCollection(collectName);
        query.put("name", "wangwu");
        update.put("$set",new BasicDBObject("age", 26));
//      update.put("$set",new BasicDBObject("sex","gril"));
        count = collection.update(query, update, upsert, multi).getN();
        return count;
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页