Aggregation.match() 在搜索 id 时不起作用
Aggregation.match() is not working when searching id
我在 spring-data-mongo
中遇到了一些奇怪的问题。
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
List<Hotel> hotels = mongoOperations.find(query, Hotel.class)
给出正确的结果 (findById
)。
然而,
MatchOperation match = Aggregation.match(Criteria.where("id").is(id));
Aggregation aggregation = Aggregation.newAggregation(match);
AggregationResults<Hotel> results = mongoOperations.aggregate(aggregation, "hotel", Hotel.class);
每次都给[]
。
有没有我遗漏的东西??
我找到了答案:
Aggregation.match(Criteria.where("_id").is(new ObjectId(hotelId)));
我在 spring-data-mongo
中遇到了一些奇怪的问题。
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
List<Hotel> hotels = mongoOperations.find(query, Hotel.class)
给出正确的结果 (findById
)。
然而,
MatchOperation match = Aggregation.match(Criteria.where("id").is(id));
Aggregation aggregation = Aggregation.newAggregation(match);
AggregationResults<Hotel> results = mongoOperations.aggregate(aggregation, "hotel", Hotel.class);
每次都给[]
。
有没有我遗漏的东西??
我找到了答案:
Aggregation.match(Criteria.where("_id").is(new ObjectId(hotelId)));