码迷,mamicode.com
首页 > 编程语言 > 详细

mongo java中and、or多条件查询

时间:2015-06-08 19:40:12      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:mongodb

        //and、or多条件联合查询
        //age条件(and条件)
        BasicDBList condList = new BasicDBList(); 
        BasicDBObject cond = new BasicDBObject();
        cond.put("$gt",0);
        cond.put("$lte",40);
        BasicDBObject composeCod = new BasicDBObject();
        composeCod.put("age", cond);
        condList.add(composeCod);
        
        //name条件
   BasicDBObject nameCond = new BasicDBObject();
   nameCond.put("name", "name");
   condList.add(nameCond);
        
   //查询条件组合(or条件)
        BasicDBObject searchCond = new BasicDBObject();
searchCond.put("$or", condList);

        DBCursor findRefResult = coll.find(searchCond);
        int i=1;
        while (findRefResult.hasNext()) { 
           System.out.println("Inserted Document: "+i); 
           System.out.println(findRefResult.next()); 
           i++;

        }


//排序操作

DBCursor cursor = collection.find(filter_dbobject).limit(10).sort(new BasicDBObject("create_time",-1));

mongo java中and、or多条件查询

标签:mongodb

原文地址:http://blog.csdn.net/subuser/article/details/46415739

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