码迷,mamicode.com
首页 > 数据库 > 详细

【MongoDB】-Java实现对mongodb的And、Or、In操作

时间:2015-08-26 15:50:17      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

1)And(同时满足多个条件时用And)

public void testAnd(){      
    DBObject queryCondition = new BasicDBObject();    
    //agender=‘female‘ AND age > 27    
    queryCondition.put("agender", "female");    
    queryCondition.put("age", new BasicDBObject("$gt", 27));    
    DBCursor dbCursor = coll.find(queryCondition);       
}    

2)Or(两个条件满足其中一个)

public void testOrSingleField(){    
    DBObject queryCondition = new BasicDBObject();          
    //age<15 OR age>27     
    BasicDBList values = new BasicDBList();    
    values.add(new BasicDBObject("age", new BasicDBObject("$gt", 27)));    
    values.add(new BasicDBObject("age", new BasicDBObject("$lt", 15)));    
    queryCondition.put("$or", values);    

    DBCursor dbCursor = coll.find(queryCondition);       
}    

3)In(需要某个字段的值在某个范围时)

public void testIn(){    
    DBObject queryCondition = new BasicDBObject();          
    //age in [13, 47]    
    BasicDBList values = new BasicDBList();    
    values.add(13);    
    values.add(47);    
    queryCondition.put("age", new BasicDBObject("$in", values));    

    DBCursor dbCursor = coll.find(queryCondition);       
}    

版权声明:本文为博主原创文章,未经博主允许不得转载。

【MongoDB】-Java实现对mongodb的And、Or、In操作

标签:

原文地址:http://blog.csdn.net/tailyou0506/article/details/48003621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!