db.ray_item_1.aggregate( [
{ $match: { _id: "581514053372"} },
{ $project: { price: 1, "skus.price":1} }
] ,{cursor:{"batchSize":1}})
这个mongodb查询语句如何使用spring-data-mongodb来写,特别是指定了子文档字段,发现一直不生效
使用MongoTemplate,你可以查阅一下相关资料
Criteria criteria = Criteria.where("_id").is("581514053372");
AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
List<AggregationOperation> list = com.google.common.collect.Lists.newArrayList();
AggregationOperation matchOperation = Aggregation.match(criteria);
list.add(matchOperation);
//这么写查询数组skus中的属性,会报错,这个该怎么写?
ProjectionOperation projectionOperation = Aggregation.project("price", "skus.price");
list.add(projectionOperation);
Aggregation aggregation = Aggregation.newAggregation(list).withOptions(aggregationOptions);
AggregationResults<Item> aggregate = mongoTemplate.aggregate(aggregation, "tb_collection", Item.class);
这么写不行,查询数组skus中的price属性会报错