Bootstrap

spring - Mongo 查询的三种方式

1.使用JSON格式字符串查询:

  • 多个字段查询:查询owner=”10035239” 或者 owner=”100000002” 并且is_pop=JD的数据,使用“或者”关系的字段使用运算符$or,“并且”关系的字段使用“,”隔开;
//多个字段查询并使用关系运算符OR
BasicQuery query = new BasicQuery("{$or:[{owner:{$eq:\"10035239\"}},{owner:  {$eq:\"100000002\"}}],is_pop:{$eq:\"JD\"}}");     
      System.out.println(JSON.toJSONString(mongoTemplate.find(query,BasicDBObject.class,"collectionName")));
  • 同一字段多个条件查询:查询10035238
//单个字段多个条件查询
BasicQuery query1 = new BasicQuery("{owner:{$lte:\"10035240\",$gt:\"10035238\"}");
        System.out.println(JSON.toJSONString(mongoTemplate.find(query1,BasicDBObject.class,"collectionName")));
}

2.通过构造嵌套对象的形式查询:

  • 通过构造一个BasicQuery对象,构造参数为条件对象(queryObjct )和显示列对象(filedsObject ),类型是BasicDBObject 。其中查询对象queryObjct 的key值是字段名,value同样是BasicDBObject 实例。值对象的key值是条件,value是值列表,单个值用BasicDBObject ,多个值用BasicDBList ;
  • 条件规范为:[{field1:{$eq:value1}},{field2:{$in:[value1,value2]}}]
@Test
    public void testMongoIn(){
        //构造查询条件对象
        BasicDBList queryList = new BasicDBList();
        queryList.add("100000019");
        queryList.add("100000017");
        BasicDBObject queryObjct = new BasicDBObject();
        BasicDBObject basicDbObject = new BasicDBObject();
        basicDbObject.put("$in",queryList);
        BasicDBObject filedsObject = new BasicDBObject();
        queryObjct.put("owner",basicDbObject);
        //构造查询结果显示列字段
        filedsObject.put("field1",1);
        filedsObject.put("field2",1);
        filedsObject.put("field3",1);
        filedsObject.put("field4",1);
        filedsObject.put("field5",1);
        filedsObject.put("field6",1);
        //构造BasicQuery
        BasicQuery basicQuery = new BasicQuery(queryObjct,filedsObject);
        //设置排序对象
        basicQuery.setSortObject((DBObject)new BasicDBObject().put("_id",-1));
        //分页
        basicQuery.skip(0);
        basicQuery.limit(1);
        List<BasicDBObject> list = mongoTemplate.find(basicQuery,BasicDBObject.class,"collectionName");
        System.err.println(JSON.toJSONString(list));
    }

3.使用spring的持久化方案

@Test
    public void testObjectCRUD() {
        //新增
        Person person = new Person("ws", 29);
        mongoTemplate.insert(person);
        System.out.println("Insert:" + JSON.toJSONString(person));
        //查询
        Person p = mongoTemplate.findById(person.getId(), Person.class);
        System.out.println("queryById:" + JSON.toJSONString(p));
        //更新第一条
        mongoTemplate.updateFirst(Query.query(Criteria.where("name").is("ws")), Update.update("age", 92), Person.class);
        List<Person> personList = mongoTemplate.find(Query.query(Criteria.where("name").is("ws")), Person.class);
        System.out.println("更新后:" + JSON.toJSONString(personList));
        //删除
        mongoTemplate.remove(p);
        //查找所有
        List<Person> personAll = mongoTemplate.findAll(Person.class);
        System.out.println("删除后:" + JSON.toJSONString(personAll));
        //删除集合
        mongoTemplate.dropCollection(Person.class);
    }
;