• 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__\

  • 相关阅读:
    推荐系统之推荐系统的分类,即分析框架
    问题解决——OpenGL超级宝典 关于gltDrawTorus的错误解决
    SICP 习题 (1.10)解题总结
    [置顶] android 自定义圆角ImageView以及锯齿的处理
    Codeforces Round #199 (Div. 2)
    数组——约瑟夫问题
    素数距离问题_ny_24.java
    [置顶] Guava学习之Splitter
    植物-常见植物:地黄、熟地黄
    植物-常见植物:仙人掌
  • 原文地址:https://www.cnblogs.com/solq/p/2512442.html
Copyright © 2020-2023  润新知