mongodb学习(java操作mongodb)

在mysql,sqlserver和orcale中都提供了java的驱动,为我们封装好了很多的接口,在java层调用起来了是很方便的。在mongodb中同样为我们提供了这样的驱动。和mysql一样,我们只需将下载好的mongo.jar进行buildPath即可,就可以调用其为我们封装的接口了。

查询已有的数据

    try {
            //建立一个mongo的连接数据库的对象
            Mongo mongo = new Mongo("127.0.0.1:27017");
            //查询所有数据库的明后才能
            List<String>dataBases = mongo.getDatabaseNames();
//          for (String name : dataBases) {
//              System.out.println(name);
//          }
            //创建连接数据库(mydb)的连接
            DB db =  mongo.getDB("mydb");
            //查询mydb中所有的集合名称
            Set<String>set =  db.getCollectionNames();
//          for (String collectionName : set) {
//              System.out.println(collectionName);
//          }
            //查询students集合中的所有数据
            DBCollection dbCollection = db.getCollection("students");
            DBCursor dbCursor = dbCollection.find();
            //将该cursor序列化成json对象
            System.out.println(JSON.serialize(dbCursor));
//          while (dbCursor.hasNext()) {
//              DBObject object =  dbCursor.next();
//              System.out.println(object.get("name"));//得到name字段的值
//          }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }

新建一个”mongnew”数据库,并且插入一条数据

//建立一个mongo的连接数据库的对象
Mongo mongo = new Mongo("127.0.0.1:27017");
//创建一个mongonew数据库
DB db = mongo.getDB("mongonew");
//创建一个集合,并且插入一条记录
DBObject dbObject = new BasicDBObject();
dbObject.put("age",22);
dbObject.put("name","张三");
List<String>fruits = new ArrayList<>();
fruits.add("orange");
fruits.add("apple");
fruits.add("water");
dbObject.put("fruits",fruits);
DBCollection teacherCollection =        db.getCollection("teachers");//得到teacher集合
teacherCollection.insert(dbObject);//插入dbObject

批量插入数据

ListdbList = new ArrayList<>();
DBObject object = null;
for (int i = 1; i < 4; i++) {
    object = new BasicDBObject();
    object.put("age",i);
    object.put("name","aName"+i);
    dbList.add(object);
}
DBCollection teacherCollection2 = db.getCollection("teachers");//得到teacher集合
teacherCollection2.insert(dbList);//批量插入dbObject,参数是一个list集合

根据条件删除数据

删除age=3的数据

DBCollection dbCollection3 = db.getCollection("teachers");
dbCollection3.remove(new BasicDBObject("age",3));

给集合批量增加属性

DBCollection dbCollection4 = db.getCollection("teachers");
DBObject updateObject = new BasicDBObject();
updateObject.put("$set",new BasicDBObject("passworld","123"));
dbCollection4.update(new BasicDBObject(),updateObject,false,true);

只查询某些字段

DBCollection dbCollection5 = db.getCollection("teachers");
DBObject keys = new BasicDBObject();
keys.put("_id",false);
keys.put("name",true);
keys.put("passworld",true);
DBCursor dbCursor =  dbCollection5.find(null,keys);
DBObject dbObjec = null;
while (dbCursor.hasNext()) {
    dbObjec = dbCursor.next();
    System.out.println(JSON.serialize(dbObjec));
}

分页查询

//分页查询
DBCollection dbCollection6 = db.getCollection("teachers");
DBCursor cursor =  dbCollection6.find().limit(2).skip(0);
DBObject dbObject = null;
while (cursor.hasNext()) {
    dbObject = cursor.next();
    System.out.println(JSON.serialize(dbObject));
}

分页查询指定的键

DBCollection dbCollection6 = db.getCollection("teachers");
DBObject keys = new BasicDBObject();
keys.put("_id",false);
keys.put("name",true);
keys.put("passworld",true);
DBCursor cursor =  dbCollection6.find(null,keys).limit(2).skip(0);
DBObject dbObject = null;
while (cursor.hasNext()) {
    dbObject = cursor.next();          System.out.println(JSON.serialize(dbObject));
}

源码下载

你可能感兴趣的:(mongoDB,java)