【发布时间】:2020-08-04 20:07:36
【问题描述】:
我的收藏中有这样的模型..
{
"_id": {
"$oid": "5f213786d0cdd10c61969fc7"
},
"stationStatus": "Online",
"metadata": {
"SerialNumber": "BP001",
"imsi": "082943327103528941"
},
"boxIdentity": "BP001",
"__v": 0
}
{
"_id": {
"$oid": "5f213786d0cdd10c61969fc7"
},
"stationStatus": "Offline",
"metadata": {
"SerialNumber": "BP002",
"imsi": "082943327103528941"
},
"boxIdentity": "BP002",
"__v": 0
}
这是我对数据执行的聚合。
var aggregation = [
{
$project: { __v: false },
},
{
$group: {
_id: { $toLower: "$stationStatus" },
stations: {
$push: "$$ROOT",
},
},
},
{
$group: {
_id: null,
stations: {
$push: {
k: "$_id",
v: "$stations",
},
},
},
},
{
$replaceRoot: {
newRoot: { $arrayToObject: "$stations" },
},
},
];
Model.aggregate(aggregation)
.then(function (res) {
console.log(res);
resolve(res);
})
.catch(function (err) {
reject(err);
});
我在数组中接收到的数据。我需要它的数据只返回对象中管道的结果,而不是最顶层数组中的对象。如何以某种方式返回结果,以便只返回数组的第一个对象,因为聚合将返回的只是一个对象。
[
{
"online": [
{
"_id": "5f213786d0cdd10c61969fc7",
"stationStatus": "Online",
"connectors": [
{
"_id": "5f213786d0cdd10c61969fc8",
...
},
{
"_id": "5f213791d0cdd10c61969fcf",
...
},
{
"_id": "5f213791d0cdd10c61969fd1",
...
}
],
"metadata": {
"SerialNumber": "BP001",
"imsi": "082943327103528941"
},
"boxIdentity": "BP001"
},
{
"_id": "5f2809d7b03827069d3b5627",
"stationStatus": "Online",
"connectors": [
{
"_id": "5f213786d0ccc310c61969fc8",
...
},
{
"_id": "5f213791d0cd340c61969fcf",
...
},
{
"_id": "5f213791d0cdd10c61369fd1",
...
}
],
"metadata": {
"SerialNumber": "BP002",
"imsi": "0963433223503528941"
},
"boxIdentity": "BP002"
}
]
}
]
【问题讨论】:
标签: mongodb express mongoose aggregation-framework