mongodb 学习(1)

阅读更多
MogonDB 学习(一)
1. 安装
安装介质http://www.mongodb.org/display/DOCS/Downloads;
驱动介质http://www.mongodb.org/display/DOCS/Downloads
我使用的是1.4.0 windown32

安装介质下载后是一个zip包,解压。

|-- bin
|   |-- mongo                              (the database shell)
|   |-- mongod                             (the core database server)
|   |-- mongos                             (auto-sharding process)
|   |-- mongodump                          (dump/export utility)
|   `-- mongorestore                       (restore/import utility)
|-- include                                (c++ driver include files)
|   `-- mongo
|       |-- client
|       |-- db
|       |-- grid
|       `-- util
|-- lib
|-- lib64

设置环境变量
MONGODB_HOME=D:\mongodb\mongodb-win32-i386-1.4.0
同时报%MONGODB_HOME%\bin放到path中

2. 启动服务端
mongod.exe -dbpath D:\mongodb\data
其中-dbpath参数表示数据文件夹的位置,同时这个文件夹必须存在,否则启动报错

3. java客户端
3.1. 创建实例
Mongo m = new Mongo();
Mongo m =  new Mongo(  "localhost" ); 
Mongo m =   new Mongo( "localhost"  , 27017 ); 

DB db = m.getDB("mydb" );
3.2. 获得集合
3.2.1. 获得所有的集合
Set colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}

3.2.2. 根据名称获得集合
DBCollection coll = db.getCollection("testCollection");
如果有名称为testCollection的集合就会使用已有的,否则就会创建一个新的集合
3.3. 插入对象
目前插入的对象好像都是json类型的对象
形如
{
    "MongoDB" : "name" ,
     "type" : "database"  ,
    "count" : 1,
    "info" : {   x : 203,
               y : 102
     }
}
private static void insert(DBCollection coll) {
BasicDBObject doc = getBasicDBObject();
coll.insert(doc);
}

private static BasicDBObject getBasicDBObject() {
BasicDBObject doc = new BasicDBObject();
doc.put("name", "MongoDB");
doc.put("type", "database");
doc.put("count", 1);
BasicDBObject info = new BasicDBObject();
info.put("x", 203);
info.put("y", 102);
doc.put("info", info);
return doc;
}

查询会发现collection 中增加一条记录

{ "_id" : "4bc3db65e7d449fb0f96183e" , "name" : "MongoDB" , "type" : "MongoDB" , "count" : 1 , "info" : { "x" : 203 , "y" : 102} }
for (  i=0; i < 100; i++) { int
    coll.insert(  BasicDBObject().append( , i)); new "i"
}
会循环插入100个记录

{ "_id" : "4bc3d39faf2a49fbde918615" , "i" : 1}
{ "_id" : "4bc3d39faf2a49fbdf918615" , "i" : 2}

{ "_id" : "4bc3d39faf2a49fbeb918615" , "i" : 99}
{ "_id" : "4bc3d39faf2a49fbec918615" , "i" : 100}
3.4. 查询
3.4.1. 统计记录个数
System.out.println(coll.getCount());
3.4.2. 根据属性查询
BasicDBObject query = new BasicDBObject();
query.put("name", "MongoDB");
DBCursor cur = coll.find(query);
while (cur.hasNext()) {
System.out.println(cur.next());
}

输出:
{ "_id" : "4bc3db65e7d449fb0f96183e" , "name" : "MongoDB" , "type" : "MongoDB" , "count" : 1 , "info" : { "x" : 203 , "y" : 102} }
query = new BasicDBObject();
query.put("i", new BasicDBObject("$gt", 50).append("$lt" , 80));  // e.g. find all where i > 50
cur = coll.find(query);
while (cur.hasNext()) {
System.out.println(cur.next());
}
输出:
{ "_id" : "4bc3d39faf2a49fbda918615" , "i" : 51}
{ "_id" : "4bc3d39faf2a49fbdb918615" , "i" : 52}
………
{ "_id" : "4bc3d39faf2a49fbf5918615" , "i" : 78}
{ "_id" : "4bc3d39faf2a49fbf6918615" , "i" : 79}

3.5. 删除
BasicDBObject query = new BasicDBObject();
query.put("name", "MongoDB");
DBCursor cur=coll.find(query);
while (cur.hasNext()) {
coll.remove(cur.next());
}
3.6. 更新
private static void saveOrUpdate(DBCollection coll) {

// is some JSON style object
// updates if exists; inserts if new
coll.save(getBasicDBObject());
BasicDBObject query = new BasicDBObject();
query.put("name", "MongoDB");
DBCursor cur=coll.find(query);
while (cur.hasNext()) {
BasicDBObject next = (BasicDBObject)cur.next();
next.put("abc","aaa");
next.put("type", "MongoDB_Test");
coll.save(next);
}

}
private static BasicDBObject getBasicDBObject() {
BasicDBObject doc = new BasicDBObject();
doc.put("name", "MongoDB");
doc.put("type", "database");
doc.put("count", 1);
BasicDBObject info = new BasicDBObject();
info.put("x", 203);
info.put("y", 102);
doc.put("info", info);
return doc;
}
对于记录会增加一个abc字段,值为aaa ,同时字段type 的值变成了MongoDB_Test
{ "_id" : "4bc3db65e7d449fb0f96183e" , "name" : "MongoDB" , "type" : "MongoDB_Test" , "count" : 1 , "info" : { "x" : 203 , "y" : 102} , "abc" : "aaa"}

3.7. 创建索引
// ascending (1) or descending (-1)
// create index on  , ascending "i"

coll.createIndex( new BasicDBObject("i" , 1))  ;

  • mongodb学习(1).rar (23.5 KB)
  • 下载次数: 17

你可能感兴趣的:(MongoDB,json)