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

你可能感兴趣的:(mongodb)