【发布时间】:2020-07-26 21:42:03
【问题描述】:
我有一个名为 Accounts 的集合,如下数据所示
/* 1 */
{
"_id" : ObjectId("5e93fea52f804ab99b54e4a2"),
"account" : "first",
"connect" : "Always",
"desc" : "first account feature first phase",
"status" : true
}
/* 2 */
{
"_id" : ObjectId("5e93fea32c804ab99b12e4d1"),
"account" : "second",
"connect" : "Sometimes",
"desc" : "second account feature first phase",
"status" : true
}
/* 3 */
{
"_id" : ObjectId("5e93fea52f804ab99b55a7b1"),
"account" : "first",
"connect" : "Sometimes",
"desc" : "first account feature second phase",
"status" : true
}
尝试以这样一种方式对数据进行分组和构造,以使查询返回如下结构的结果
/* First Row */
"account" : "first",
[
{
_id:ObjectId("5e93fea52f804ab99b54e4a2")
"connect" : "Always",
"desc" : "first account feature first phase",
"status" : true
},
{
_id:ObjectId("5e93fea52f804ab99b55a7b1")
"connect" : "Sometimes",
"desc" : "first account feature second phase",
"status" : true
},
]
/* Second Row */
"account" : "second",
[
{
_id:ObjectId("5e93fea32c804ab99b12e4d1")
"connect" : "Always",
"desc" : "second account feature first phase",
"status" : true
},
]
希望与 Account 分组,并且相应的帐户行应该具有该 Id 的其他相关数据。
我尝试了什么
我试着写在下面的查询
db.accounts.aggregate(
{
$match : {account : {$in: ["first", "second"]}}
}
,
{
$group : {
_id : "$account"
}
}
);
但这部分正确返回帐户列表,例如第一、第二但不相关的属性。
谢谢
【问题讨论】:
标签: mongodb mongoose group-by mongodb-query aggregation-framework