Mongodb学习笔记

总结下这几天Mongodb学习笔记

   /**
     * 获取MongoClient
     * @author xuyw 
     * @email xyw10000@163.com 
     * @param host
     * @param port
     * @return
     */
    public static MongoClient getMongoClient(String host, int... port) {
        MongoClient mongoClient = null;
        int portlen = 0;
        try {
            if (port != null)
                portlen = port.length;
            switch (portlen) {
            case 0:
                if (host != null && !"".equals(host)) {
                    // 方式二(指定ip):
                    mongoClient = new MongoClient(host);
                } else {
                    // 直接连接单一mongodb服务器
                    mongoClient = new MongoClient();
                }
                break;
            case 1:
                // 方式二(指定ip、端口):
                mongoClient = new MongoClient(host, port[0]);
                break;
            default:
                // 方式三:连接到mongodb服务器集群(会自动发现主服务器)
                List list = new ArrayList();
                String[] hostip = host.split(",");
                for (int i = 0; i < portlen; i++) {
                    ServerAddress saddress = new ServerAddress(hostip[i],
                            port[i]);
                    list.add(saddress);
                }
                mongoClient = new MongoClient(list);
                break;

            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return mongoClient;
    }

``/**
     * 获取Db
     * 
     * @param dbname
     * @param host
     * @param port
     * @return
     */
    public static DB getDB(String dbname, String host, int... port) {
        MongoClient mongoClient = getMongoClient(host, port);
        DB db = mongoClient.getDB(dbname);
        return db;
    }

MongoClient mongoClient = MongoDbUtil.getMongoClient(“192.168.1.101”,
27017);
// 查询所有的Database
for (String name : mongoClient.getDatabaseNames()) {
System.out.println(“databaseName: ” + name);
}
// 获取指定数据库
DB db = mongoClient.getDB(“xuywdb”);
// 查询所有的聚集集合类似oracle里面的表名
Set colls = db.getCollectionNames();

    for (String s : colls) {
        System.out.println(s);
    }


//新增2条记录
public void inserUser(DBCollection t_user) {
DBObject user = new BasicDBObject();
user.put(“username”, “张三”);
user.put(“userage”, 18);
user.put(“usersex”, “男”);
DBObject user2 = new BasicDBObject();
user2.put(“username”, “小红”);
user2.put(“userage”, 19);
user2.put(“usersex”, “女”);
t_user.insert(user);
t_user.insert(user2);
}

//查询所有返回一个游标
DBCollection t_user = db.getCollection("t_user");
        DBCursor cursor =t_user.find();
        while(cursor.hasNext()){
            System.out.println(cursor.next());
        }

结果输出

{ "_id" : { "$oid" : "5550bf41d975cf177eab8d1f"} , "username" : "张三" , "userage" : 18 , "usersex" : "男"}
{ "_id" : { "$oid" : "5550bf41d975cf177eab8d20"} , "username" : "小红" , "userage" : 19 , "usersex" : "女"}
json 操作

DBObject 转json

JSON.serialize(dBObject )

json转DBObject 
JSON.parse("{ \"name\" : \"xuyw\" , \"age\" : 23}")
展开阅读全文

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

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读