mongo java中and、or多条件查询

        //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)