mongodb在java中的查询

时间:2023-12-04 11:25:02

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;
}