有以下book 表中的数据,如何使用mongodb查询出根据type, category第二项分组的,category 第3项的最大值?
即如下格式:
borrowed:
math
number:3
borrowed
english
number:5
book表数据
{
"_id": 1,
"category": ["1", "math", "3"],
"year": 2020,
"type": "borrowed"
}
{
"_id": 2,
"category": ["1", "english", "5"],
"year": 2019,
"type": "borrowed"
}
{
"_id": 2,
"category": ["1", "english", "2"],
"stock": 4,
"type": "in store"
}
{
"_id": 2,
"category": ["1", "english", "5"],
"year": 1999,
"type": "borrowed"
}
写出来的语句如下,但索引取值不对,请各位大神帮忙解答。
db.book.aggregate( [
{ $group: { _id: {type:'$type', book:'$category.1' }, number: { $max: $category.2 } } }
]);