[英]Fetch data from mongodb in three different field according to condition
i have a User collection having structure as below 我有一个用户集合,其结构如下
{
"_id" : ObjectId("596845524e20b8328d61b01f"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"password" : "$2a$10$.WG",
"user_type" : 1,
"full_name" : "c f"
}
},
{
"_id" : ObjectId("596845524e20b8328d61b01d"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"user_type" : 2,
"full_name" : "c f"
}
},
{
"_id" : ObjectId("596845524e20b8328d61b01g"),
"created_at" : ISODate("2017-07-10T04:15:14.327Z"),
"status" : 1,
"updated_at" : ISODate("2017-08-22T07:29:44.000Z"),
"is_activated" : 0,
"totalrating" : 0,
"is_admin" : 0,
"basic_detail" : {
"first_name" : "c",
"last_name" : "f",
"user_type" : 3,
"full_name" : "c f"
}
}
i want to fetch data through aggreagte query and want data like below 我想通过aggreagte查询获取数据,并希望像下面这样的数据
{
"totaluser":3,
"totalho":1,//all those record having user_type=1,
"totaltr":2,//all those record having user_type=2,
"totalcf":3,//all those record having user_type=3
}
trying to do same with the help of group but not able to do please help I am using mongodb 3.4 试图在小组的帮助下做同样的事情,但不能做,请帮助我正在使用mongodb 3.4
[{ $facet:
{
totaluser: [ { $count: "totaluser" } ],
totalho: [ {$match:{"basic_detail.user_type":1}},{ $count: "totalho" }],
//other fields
}
},
{
$addFields:{
"totaluser": {
$let:{
vars:{totaluserObj:{ $arrayElemAt: [ "$totaluser", 0 ] }},
in:"$$totaluserObj.totaluser"
}
},
"totalho": {
$let:{
vars:{totalhoObj:{ $arrayElemAt: [ "$totalho", 0 ] }},
in:"$$totalhoObj.totalho"
}
}
}
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.