【IT168 技术】MongoDB官网:http://www.mongodb.org/
MongoDB J驱动:http://www.mongodb.org/display/DOCS/Drivers
MongoDB Java文档:http://www.mongodb.org/display/DOCS/Java+Tutorial
1、MongoDB 连接:
连接数据库如果不存在,会自动生成数据库。
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
Mongo m = new Mongo();
// or
Mongo m = new Mongo( "localhost" );
// or
Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB( "mydb" );
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
Mongo m = new Mongo();
// or
Mongo m = new Mongo( "localhost" );
// or
Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB( "mydb" );
设置用户名密码连接:有就返回true,否则返回false。
boolean auth = db.authenticate(myUserName, myPassword);
2、MongoDB 获取所有的数据库
Mongo m = new Mongo();
for (String s : m.getDatabaseNames()){
System.out.println(s);
}
for (String s : m.getDatabaseNames()){
System.out.println(s);
}
删除数据库:
Mongo m = new Mongo();
m.dropDatabase("myDatabaseName");
m.dropDatabase("myDatabaseName");
3、MongoDB表
获取数据库的所有表:
Set<String> colls = db.getCollectionNames();
for (String s : colls) {
System.out.println(s);
}
for (String s : colls) {
System.out.println(s);
}
获取表的索引:
List<DBObject> list = coll.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
for (DBObject o : list) {
System.out.println(o);
}
获取表的所有记录:
DBCursor cur = coll.find();
while(cur.hasNext()) {
System.out.println(cur.next());
}
while(cur.hasNext()) {
System.out.println(cur.next());
}
获取表的记录数量:
coll.find().count();
coll.find(new BasicDBObject("i", 26)).count(); //(可以在find里添加条件 )
coll.find(new BasicDBObject("i", 26)).count(); //(可以在find里添加条件 )
获取查询的第一条记录:
DBObject firstDoc = coll.findOne();
//findOne() 返回一个记录,而find()返回的是DBCursor游标对象。
//findOne() 返回一个记录,而find()返回的是DBCursor游标对象。
4、查询
条件查询:
BasicDBObject condition = new BasicDBObject();
condition.put("name", "bruce");
condition.put("age", 26);
coll.find(condition);
condition.put("name", "bruce");
condition.put("age", 26);
coll.find(condition);
分页查询:
DBCursor cursor = coll.find().skip(0).limit(10);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
条件查询:
BasicDBObject condition = new BasicDBObject();
condition.put("age", new BasicDBObject("$gt", 50));
coll.find(condition);
比较符
"$gt": 大于
"$gte":大于等于
"$lt": 小于
"$lte":小于等于
"$in": 包含
//以下条件查询20<age<=30
condition.put("age", new BasicDBObject("$gt", 20).append("$lte", 30));
condition.put("age", new BasicDBObject("$gt", 50));
coll.find(condition);
比较符
"$gt": 大于
"$gte":大于等于
"$lt": 小于
"$lte":小于等于
"$in": 包含
//以下条件查询20<age<=30
condition.put("age", new BasicDBObject("$gt", 20).append("$lte", 30));
$exists
用来判断一个元素是否存在,如:
db.things.find( {a:{$exists:true}}); // 如果存在元素a,就返回true
$type
基于 bson type来匹配一个元素的类型,像是按照类型ID来匹配,不过我没找到bson类型和id对照表。
db.things.find({a:{$type:2}}); // matches if a is a string
正则表达式
查询所有名字匹配 /joh?n/i 的记录。
Pattern pattern = Pattern.compile("joh?n", CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject("name", pattern);
DBCursor cursor = coll.find(query);
BasicDBObject query = new BasicDBObject("name", pattern);
DBCursor cursor = coll.find(query);
5、数据插入
批量插入:
List datas = new ArrayList();
for (int i=0; i < 100; i++) {
BasicDBObject bo = new BasicDBObject();
bo.put("name", "bruce");
bo.append("age", i);
datas.add(bo);
}
coll.insert(datas);
for (int i=0; i < 100; i++) {
BasicDBObject bo = new BasicDBObject();
bo.put("name", "bruce");
bo.append("age", i);
datas.add(bo);
}
coll.insert(datas);
单个插入:
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);
coll.insert(doc);
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);
coll.insert(doc);
6、索引
coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending -1 descending
获取所有的索引:
List<DBObject> list = coll.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
for (DBObject o : list) {
System.out.println(o);
}