admin管理员组文章数量:1429000
I am trying to groups documents by two fields, field1
and field2
.
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$field2"
},
count: {
$sum: 1
}
}
}])
Which works fine producing the expected results.
But I want to re run the above in a loop, each run will have different $field2
, so the following is failing, how can I it be done? Thanks
const field3 = 'someValue'; // <<--- will change in every loop run ---
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$$field3" //<<----------------
},
count: {
$sum: 1
}
}
}])
I am trying to groups documents by two fields, field1
and field2
.
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$field2"
},
count: {
$sum: 1
}
}
}])
Which works fine producing the expected results.
But I want to re run the above in a loop, each run will have different $field2
, so the following is failing, how can I it be done? Thanks
const field3 = 'someValue'; // <<--- will change in every loop run ---
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$$field3" //<<----------------
},
count: {
$sum: 1
}
}
}])
Share
Improve this question
edited Sep 28, 2016 at 8:10
DAXaholic
35.6k6 gold badges82 silver badges77 bronze badges
asked Sep 27, 2016 at 22:02
Fred J.Fred J.
6,04913 gold badges60 silver badges113 bronze badges
1 Answer
Reset to default 5The following should work for you
var field3 = 'someValue';
myCollection.aggregate([{
$group: {
_id: {
group1: "$field1",
group2: "$" + field3,
},
count: {
$sum: 1
}
}
}])
本文标签:
版权声明:本文标题:javascript - Mongodb aggregation, Use value from a variable in the expression of $group _id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451281a2658898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论