搜尋

首頁  >  問答  >  主體

Spring + MongoDB 利用MongoTemplate查詢的問題

db中有符合該條件的資料。

Query query=new Query(
    Criteria.where("AAA").is(XXobj.getAAA()).
    orOperator(Criteria.where("BBB").is(XXobj.getBBB()))
    );

find()方法:

List<XXObject> result = mongoTemplate.find(query, XXObject.class);
if(result!=null && !result.isEmpty()){
    return result.get(0);
}

findOne()方法:

XXObject obj = mongoTemplate.findOne(query, XXObject.class);
if(obj!=null){
    return obj;
}

問:
為什麼同樣的條件,這兩個查詢的結果卻不一樣? (我遇到的問題是findOne查詢結果為空)。

附:
官方文件對findOne和find的說明:
findOne Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type.

find Map the results of an ad-hoc query on the collection to a List of the specified type.

看起來沒什麼差別。

伊谢尔伦伊谢尔伦2848 天前610

全部回覆(0)我來回復

無回覆
  • 取消回覆