mongo java中and、or多條件查詢,mongojava

來源:互聯網
上載者:User

mongo java中and、or多條件查詢,mongojava
        //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));

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.