步骤一、启动数据库
[root@master ~]# cd /opt/mongodb
[root@master mongodb]# ls
bin data GNU-AGPL-3.0 log README THIRD-PARTY-NOTICES
[root@master mongodb]# cd data/
[root@master data]# ls
mongod.lock test.0 test.1 test.ns _tmp
[root@master data]# cd ../bin/
[root@master bin]# ./mongod --dbpath=/opt/mongodb/data/ --logpath=/opt/mongodb/log/mongodb.log
all output going to: /opt/mongodb/log/mongodb.log
另开一个窗口,监视日志进程
[root@master ~]# tail -f /opt/mongodb/log/mongodb.log
Wed Jan 6 11:54:40 MongoDB starting : pid=3679 port=27017 dbpath=/opt/mongodb/data/ 64-bit
Wed Jan 6 11:54:40 db version v1.6.3,pdfile version 4.5
Wed Jan 6 11:54:40 git version: 278bd2ac2f2efbee556f32c13c1b6803224d1c01
Wed Jan 6 11:54:40 sys info: Linux domU-12-31-39-06-79-A1 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41
Wed Jan 6 11:54:40 [initandlisten] waiting for connections on port 27017
Wed Jan 6 11:54:40 [websvr] web admin interface listening on port 28017
步骤二、使用maven构建编码环境
package cn.com.mongodbtest.mongodbtest;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
/**
* Hello world!
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.0</version>
</dependency>
*/
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
Mongo mongo = new Mongo("192.168.1.109");
// * Mongo mongo2 = new Mongo( "127.0.0.1",27017 );
// * Mongo mongo3 = new Mongo( new DBAddress( "127.0.0.1:27017","test"
// ) )
List<String> dbNames = mongo.getDatabaseNames();
System.out.println(dbNames);// [test,admin,local]
DB db = mongo.getDB(dbNames.get(0));
System.out.println(db.getCollectionNames());// [c1,system.indexes]
DBCollection conn = db.getCollection("c1");
System.out.println(conn.getName());// c1
// 一、增加
// DBObject doc = new BasicDBObject();
// doc.put( "foo","bar1" );
// conn.insert(doc);
// BSONObject
// DBObject doc = new BasicDBObject();
// doc.put( "name","tomcat" );
// Map map = new HashMap();
// map.put("school","华北航天工业学院");
// List list =new ArrayList();
// list.add("邮箱1");
// list.add("邮箱2");
// map.put("email",list);
// doc.putAll(map);
// conn.insert(doc);
// 二、修改
/**
* @param q search query for old object to update
* @param o object with which to update <tt>q</tt>
* @param upsert if the database should create the element if it does not exist
* @param multi if the update should be applied to all objects matching (db version 1.1.3 and above)
*/
// BasicDBObject old = new BasicDBObject();
// old.put( "foo","bar" );
// DBObject newobj = new BasicDBObject();
// newobj.put( "性别","保密" );
// newobj.put( "name","tom" );
// DBObject query = conn.findOne(old);
// // 这里的q对象一定要是find出的而不是new的,否则多字段的情况下就会丢失其它字段信息
// conn.update(query,newobj);
// 三、删除
//DBObject delobj = new BasicDBObject();
//delobj.put("foo","bar1");
//conn.remove(delobj);
// 四、遍历
DBCursor cursor = conn.find();
// System.out.println(cursor.explain());
while (cursor.hasNext()) {
DBObject obj = cursor.next();
System.out.println(obj);
}
// 五、统计总数
System.out.println(conn.count());
DBObject where = new BasicDBObject("name","tomcat");
System.out.println(conn.count(where));
//六、建立索引
DBObject index = new BasicDBObject("foo","bar1");
conn.createIndex(index);
DBObject save = new BasicDBObject("name","tomcat");
save.put("age",24);
save.put("生日",123);
conn.save(save);
// conn.drop();
}
}