Pattern john = Pattern.compile("joh?n", CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject("name", john);
// finds all people with "name" matching /joh?n/i
DBCursor cursor = collection.find(query);
(2)日期、时间
java驱动使用java.util.Date做日期:
Date now = new Date();
BasicDBObject time = new BasicDBObject("ts", now);
collection.save(time);
(3)内嵌文档
{
"x" : {
"y" : 3
}
}
上边这种文档结构可以用下边这种方式存储
BasicDBObject y = new BasicDBObject("y", 3);
BasicDBObject x = new BasicDBObject("x", y);
(4)数组
扩展自List的结构都可以用数组形式存储
ArrayList x = new ArrayList();
x.add(1);
x.add(2);
x.add(new BasicDBObject("foo", "bar"));
x.add(4);
BasicDBObject doc = new BasicDBObject("x", x); 四、MongoOptions
java驱动中,可以在获取mongo实例时,指定一些参数,如下:
ServerAddress serverAddress=new ServerAddress("127.0.0.1",27017);
MongoOptions mongoOptions=new MongoOptions();
Mongo mongo=new Mongo(serverAddress,mongoOptions);
参数列表如下:
#控制系统在发生连接错误时是否重试 ,默认为false --boolean
mongo.options.autoConnectRetry=false
#每个主机允许的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住 ,默认为10 --int
mongo.options.connectionsPerHost=10
#设置等待获取连接池连接的最大数,比如,connectionsPerHost 是10,threadsAllowedToBlockForConnectionMultiplier 是5,则最多有50个线程可以等待获取连接 --int
mongo.options.threadsAllowedToBlockForConnectionMultiplier=5
#被阻塞线程从连接池获取连接的最长等待时间(ms) --int
mongo.options.maxWaitTime
#在建立(打开)套接字连接时的超时时间(ms),默认为0(无限) --int
mongo.options.connectTimeout=0
#套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默认为0(无限) --int
mongo.options.socketTimeout=0
#This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE). defaults to false --boolean
mongo.options.socketKeepAlive=false
#Override the DBCallback factory. Default is for the standard Mongo Java driver configuration --DBCallbackFactory
mongo.options.dbCallbackFactory
#//指明是否允许驱动从次要节点或者奴隶节点读取数据,默认为false --boolean
mongo.options.slaveOk=false
#如果为true,驱动每次update后会发出一个getLastError命令来保证成功,默认为false --boolean
mongo.options.safe=false
#If set, the w value of WriteConcern for the connection is set to this. Defaults to 0; implies safe = true --int
mongo.options.w=0
#If set, the wtimeout value of WriteConcern for the connection is set to this. Defaults to 0; implies safe = true --int
mongo.options.wtimeout=0
#Sets the fsync value of WriteConcern for the connection. Defaults to false; implies safe = true --boolean
mongo.options.fsync=false 五、杂
ObjectId由4部分编码而成:当前时间、机器标识、进程号和自增的整数。
排序加翻页
collection.find(queryObject).sort(new BasicDBObject().append("pt", "-1").append("ct", "1")).skip(10).limit(10);
小记录:
从名为tag的数组中删除值为123的元素
BasicDBObject updateObject = new BasicDBObject().append("$pull", new BasicDBObject().append("tag","123"));
//queryObject是查询条件
dbCollection.updateMulti(queryObject, updateObject); 六、操作
查询数量,以下三种方式未发现明显性能差别,相对于在mongoVUE里查询,都表现较慢
// 方式一
long count=collection.find(queryObject).count();
//方式二,可以查询某个字段的数量
long count=collection.getCount(queryObject,new BasicDBObject().append("_id", 1));
//方式三
long count=collection.count(queryObject);
批量从数组中删除元素
//从name等于xiaoming的文档中批量删除tag数组中的r521元素
collection.updateMulti(new BasicDBObject().append("name", "xiaoming"),
new BasicDBObject().append("$pull", new BasicDBObject().append("tag","r521")));
存图片文件
GridFS gfsPhoto = new GridFS(db, "photo");
DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) {
System.out.println(cursor.next());
}
存到另外一个文件中
String newFileName = "mkyong-java-image";
GridFS gfsPhoto = new GridFS(db, "photo");
GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file
删除图片
String newFileName = "mkyong-java-image";
GridFS gfsPhoto = new GridFS(db, "photo");
gfsPhoto.remove(gfsPhoto.findOne(newFileName));
to be continued...