• sfs2x 连接 mongodb


    void initMongodb() {
            Mongo mongo;
            try {
                trace("\n++++++++++++++++++++++initMongodb++++++++++++++++++++++++\n");
                mongo = new Mongo("localhost", 10011);
    
                DB db = mongo.getDB("yourdb");
    
                // 从Mongodb中获得名为yourColleection的数据集合,如果该数据集合不存在,Mongodb会为其新建立
    
                DBCollection collection = db.getCollection("yourCollection");// 控制操作交给
                                                                                // collection
    
                // 插入方式一
                BasicDBObject document = new BasicDBObject();
                document.put("id", 1001);
                document.put("msg", "hello world mongoDB in Java");
                collection.insert(document);
    
                /*
                 * //插入方式二 BasicDBObjectBuilder documentBuilder =
                 * BasicDBObjectBuilder.start() .add("database", "mkyongDB")
                 * .add("table", "hosting"); BasicDBObjectBuilder
                 * documentBuilderDetail = BasicDBObjectBuilder.start()
                 * .add("records", "99") .add("index", "vps_index1") .add("active",
                 * "true"); documentBuilder.add("detail",
                 * documentBuilderDetail.get());
                 * collection.insert(documentBuilder.get());
                 * 
                 * //插入方式三
                 * 
                 * Map documentMap =new HashMap(); documentMap.put("database",
                 * "mkyongDB"); documentMap.put("table", "hosting");
                 * 
                 * Map documentMapDetail =new HashMap();
                 * 
                 * documentMapDetail.put("records", "99");
                 * documentMapDetail.put("index", "vps_index1");
                 * documentMapDetail.put("active", "true");
                 * documentMap.put("detail", documentMapDetail);
                 * 
                 * collection.insert(new BasicDBObject(documentMap));
                 * 
                 * //插入方式四
                 * 
                 * String json ="{'database' : 'mkyongDB','table' : 'hosting',"+
                 * "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}"
                 * ; DBObject dbObject =(DBObject)JSON.parse(json);
                 * collection.insert(dbObject);
                 */
    
                // 创建要查询的document
                BasicDBObject searchQuery = new BasicDBObject();
                searchQuery.put("id", 1001);
    
                DBCursor cursor = collection.find(searchQuery);
    
                while (cursor.hasNext()) {
                    trace(cursor.next());
                }
    
                // 获取数据库所有表
                Set<String> collections = db.getCollectionNames();
                for (String collectionName : collections) {
                    System.out.println(collectionName);
                }
    
                mongo.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (MongoException e) {
                e.printStackTrace();
            }
    
        }

    首先下载java 驱动,,,mongodb 官方网里有提供。。。

    sfs2x 也放一份 \SFS2X\extensions\__lib__\

  • 相关阅读:
    Linux信号列表(zz)
    TCP状态转移图学习总结
    UNP学习笔记之四select和poll
    RSS2.0结构
    UNP学习笔记二简单的并发服务器(concurrent servers)
    js面向对象基础(zz)
    libevent introduction
    Linux下Makefile的automake生成全攻略(zz)
    UNP学习笔记之三POSIX Signal Handling
    关于网络编程(服务端)的一些笔记(zz)
  • 原文地址:https://www.cnblogs.com/solq/p/2512442.html
Copyright © 2020-2023  润新知