[英]How to count items in nested array of mongo document using spring data?
I have to count the number of properties
my Product
has. 我必须计算我的
Product
具有的properties
数量。 I am using following document structure: 我正在使用以下文档结构:
{
"_id": ObjectId("0000000000000000000002"),
"name" : "Test",
"properties" : [
ObjectId("0000000000000000000003"),
ObjectId("0000000000000000000004")
]
}
How to do it? 怎么做?
Update: 更新:
My try: 我的尝试:
...
@Autowired
private MongoOperations mongoOperations;
...
mongoOperations.aggregate(
new BasicQuery("{$match: {\"_id\" : " + new ObjectId(productId) + "}}," +
"{$unwind: \"$properties\"}," +
"{$project: {count:{$add:1}}}," +
"{$group: {_id: null, number: {$sum: \"$count\" }}} "),
Product.class);
I am getting error from IDE: 我从IDE中收到错误消息:
aggregate in MaongoOperations cannot be applied to or.springframework.data.mongodb.core.query.BasicQuery
How to fix it? 如何解决? Is it the easiest way to count properties using Sparing data?
这是使用Sparing数据计算属性的最简单方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.