启动db数据服务:1> .\mongod.exe --config E:\mongoDb\mongo.config
2> .\mongod --dbpath E:\mongoDb\db
注:第二步要重新开一个窗口
// ---------------------new Mongo--------------------------------
Mongo mongo = new Mongo("127.0.0.1", 27017);
// 选择数据库,如果没有这个数据库的话,会自动建立 (相当于关系数据库的数据库实例)
DB db = mongo.getDB("test");
// ---------------------mongoclient------------------------------
// DB db= mongoclient.getDB(dbname);
// --------------------------------------------------------------
// 建立一个集合,和数据库一样,如果没有,会自动建立 (相当于关系数据库表)
DBCollection DBcollectiontable = db.getCollection("doc");
BasicDBObject dcobject = new BasicDBObject();
dcobject.put("name", "b5m");
dcobject.put("age", 5);
dcobject.put("sex", "gril");
BasicDBObject infoobject = new BasicDBObject();
infoobject.append("x", "x");
infoobject.append("y", "y");
infoobject.append("y", "y");
dcobject.append("infoobject", infoobject);
// ----------------------------插入记录-----------------------------
DBcollectiontable.insert(dcobject);
//-------------------------查找第一条记录----------------------------
DBObject doc = DBcollectiontable.findOne();
System.out.println("doc的结果 :" + doc);
//--------------------------插入多条数据----------------------------
for (int i = 0; i < 100; i++) {
DBcollectiontable.insert(new BasicDBObject().append("i", i));
}
// ----------------------------获取文档条数------------------------
System.out.println("读取table数据条数:" + DBcollectiontable.getCount());
// ---------------------------------读取所有的文档目录---------------
DBCursor sort = DBcollectiontable.find();
while(sort.hasNext()){
System.out.println("第"+sort.numSeen()+"数据条数:"+sort.next());
}
// ---------------------------------根据条件查询一条记录 -------------
BasicDBObject query = new BasicDBObject();
query.put("i", 50);
sort = DBcollectiontable.find(query);
try {
while (sort.hasNext()) {
System.out.println("根据条件查询一条记录 :" + sort.next());
}
} finally {
sort.close();
}
// ---------------------------------根据条件查询查找 i>50的记录-----------
query = new BasicDBObject();
query.put("i", new BasicDBObject().append("$gt", 98));
sort=DBcollectiontable.find(query);
try {
while (sort.hasNext()) {
System.out.println("根据条件查询查找 i>50的记录 :" + sort.next());
}
} finally {
sort.close();
}
// ---------------------------------根据条件查询查找95<i<=96的记录---------
query = new BasicDBObject();
query.put("i", new BasicDBObject().append("$gt", 95).append("$lte", 96));
sort=DBcollectiontable.find(query);
try {
while (sort.hasNext()) {
System.out.println("根据条件查询查找95<i<=96的记录 :" + sort.next());
}
} finally {
sort.close();
}
// ---------------------------------根据条件查询查找更新后的记录-----------
query = new BasicDBObject();
query.put("i", 50);
BasicDBObject update = new BasicDBObject();
update.put("i", 500);
DBObject objectupdated= DBcollectiontable.findAndModify(query, update);
System.out.println("根据条件查询查找更新后的记录 :" + objectupdated);
// ---------------------------------删除记录----------------------------
query = new BasicDBObject();
query.append("i", 99);
DBcollectiontable.findAndRemove(query);
//--------------------------------删除数据库 -----------------------------
db.dropDatabase();
mongo.dropDatabase("test");