mongodb在java中的查询

mongodb 根据_id 查询记录:

public Price queryPriceById(String id) throws Exception {
  return mongoTemplate.findById(new ObjectId(id), Price.class, PRICE_COLLECTION);
}

根据集合中的某个字段查询:

    public Price queryPriceByName(String name) throws Exception {
        Price price = null;
        price = mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), Price.class, PRICE_COLLECTION);
        return price;
    }

 

posted @ 2016-09-05 00:25  zhoudingzhao  阅读(1215)  评论(0编辑  收藏  举报