mongo的命令,查询语句可以参看 mongodb教程 ,现在我们来看下使用java 实现mongodb的条件操作。
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient();
DB test = client.getDB("test");
DBCollection user = test.getCollection("user");
//查找年龄小于30的user
BasicDBObject query = new BasicDBObject();
query.append("age", new BasicDBObject("$lt",30));
DBCursor cursor = user.find(query);
while(cursor.hasNext()){
DBObject ob = cursor.next();
System.out.println(ob.toString());
}
}
运行结果
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea1"} , "name" : "李四" , "age" : 29.0 , "job" : "php 开发" , "phone" : "18984834098" , "addr" : "广东深圳南山"}
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea4"} , "name" : "钱二" , "age" : 26.0 , "job" : "销售" , "phone" : "18989834968" , "addr" : "广东珠海"}
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea6"} , "name" : "吴奇" , "age" : 17.0 , "job" : "学生" , "phone" : "18735834098" , "addr" : "广东韶关"}
{ "_id" : { "$oid" : "570f8be07cf08b02f5e52ea8"} , "name" : "冯峰" , "age" : 23.0 , "job" : "java 开发" , "phone" : "13689834098" , "addr" : "广东广州越秀"}
使用正则查询,类似sql中的like
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient();
DB test = client.getDB("test");
DBCollection user = test.getCollection("user");
// 查找 addr中包含深圳的数据
BasicDBObject query = new BasicDBObject();
query.append("addr", Pattern.compile(".*深圳.*"));
DBCursor cursor = user.find(query);
while (cursor.hasNext()) {
DBObject ob = cursor.next();
System.out.println(ob.toString());
}
}
结果
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea0"} , "name" : "张三" , "age" : 34.0 , "job" : "java 开发" , "phone" : "18989834028" , "addr" : "广东深圳福田"}
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea1"} , "name" : "李四" , "age" : 29.0 , "job" : "php 开发" , "phone" : "18984834098" , "addr" : "广东深圳南山"}
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea2"} , "name" : "王五" , "age" : 38.0 , "job" : "部门经理" , "phone" : "18981834098" , "addr" : "广东深圳罗湖"}
最后看下 Or 查询
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient();
DB test = client.getDB("test");
DBCollection user = test.getCollection("user");
// 查找 addr中包含深圳的数据
BasicDBObject query = new BasicDBObject();
BasicDBList list = new BasicDBList();
list.add(new BasicDBObject("name","李四"));
list.add(new BasicDBObject("job","java 开发"));
query.append("$or", list);
DBCursor cursor = user.find(query);
while (cursor.hasNext()) {
DBObject ob = cursor.next();
System.out.println(ob.toString());
}
}
结果
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea0"} , "name" : "张三" , "age" : 34.0 , "job" : "java 开发" , "phone" : "18989834028" , "addr" : "广东深圳福田"}
{ "_id" : { "$oid" : "570f8bdf7cf08b02f5e52ea1"} , "name" : "李四" , "age" : 29.0 , "job" : "php 开发" , "phone" : "18984834098" , "addr" : "广东深圳南山"}
{ "_id" : { "$oid" : "570f8be07cf08b02f5e52ea8"} , "name" : "冯峰" , "age" : 23.0 , "job" : "java 开发" , "phone" : "13689834098" , "addr" : "广东广州越秀"}
可以看到其实还是很简单的,只是写条件的是否太啰嗦了