我正试图确定一场演出中订购了多少个座位。
每个显示文档中都有一个“ showTakenSeats”对象。
以下是初始数据:
[
{
"_id": "5b658d37692f2e3c881960cb",
"_MovieId": "5b5835dd86ab401494263861",
"_TheaterId": "5b3dfeb217bc8c23f0e7ec3f",
"dateTime": "2018-08-04T14:00:00.000Z",
"showTakenSeats": {
"0-0": "5b658f79dd8bb938289e2e0b",
"0-1": "5b658f79dd8bb938289e2e0b",
"0-2": "5b658f79dd8bb938289e2e0b"
},
"__v": 0,
},
{
"_id": "5b658d4a692f2e3c881960cc",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b45d269a53b0c05f8959260",
"dateTime": "2018-08-04T13:00:00.000Z",
"showTakenSeats": {
"0-0": "5b65904bdd8bb938289e2e0d",
"0-1": "5b65904bdd8bb938289e2e0d",
"0-2": "5b65904bdd8bb938289e2e0d"
},
"__v": 0,
},
{
"_id": "5b658fd1dd8bb938289e2e0c",
"_MovieId": "5b575452478afb72ec65f8f6",
"_TheaterId": "5b45d219a53b0c05f895925f",
"dateTime": "2018-08-04T20:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
},
{
"_id": "5b6593d80d48d9394877dd09",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b446a8768db4e2e980723ab",
"dateTime": "2018-08-05T10:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
},
{
"_id": "5b65943b0d48d9394877dd0a",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b446a8768db4e2e980723ab",
"dateTime": "2018-08-03T22:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
}
]
这是我的代码:
Show.aggregate([
{
$addFields: {'count':Object.keys('$showTakenSeats').length}
}
], function (err, result) {
if (err) {
//next(err);
return res.json(err);
} else {
res.json(result);
}
});
结果如下:
[
{
"_id": "5b658d37692f2e3c881960cb",
"_MovieId": "5b5835dd86ab401494263861",
"_TheaterId": "5b3dfeb217bc8c23f0e7ec3f",
"dateTime": "2018-08-04T14:00:00.000Z",
"showTakenSeats": {
"0-0": "5b658f79dd8bb938289e2e0b",
"0-1": "5b658f79dd8bb938289e2e0b",
"0-2": "5b658f79dd8bb938289e2e0b"
},
"__v": 0,
"count": 15
},
{
"_id": "5b658d4a692f2e3c881960cc",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b45d269a53b0c05f8959260",
"dateTime": "2018-08-04T13:00:00.000Z",
"showTakenSeats": {
"0-0": "5b65904bdd8bb938289e2e0d",
"0-1": "5b65904bdd8bb938289e2e0d",
"0-2": "5b65904bdd8bb938289e2e0d"
},
"__v": 0,
"count": 15
},
{
"_id": "5b658fd1dd8bb938289e2e0c",
"_MovieId": "5b575452478afb72ec65f8f6",
"_TheaterId": "5b45d219a53b0c05f895925f",
"dateTime": "2018-08-04T20:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
"count": 15
},
{
"_id": "5b6593d80d48d9394877dd09",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b446a8768db4e2e980723ab",
"dateTime": "2018-08-05T10:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
"count": 15
},
{
"_id": "5b65943b0d48d9394877dd0a",
"_MovieId": "5b584eb186ab401494263868",
"_TheaterId": "5b446a8768db4e2e980723ab",
"dateTime": "2018-08-03T22:00:00.000Z",
"showTakenSeats": {},
"__v": 0,
"count": 15
}
]
计数结果不一致。似乎“ Object.keys('$showTakenSeats').length
”始终返回15。而不是应该不同的实际值。
计数结果应为(3,3,0,0,0)
有人知道出什么问题吗? 谢谢!
答案 0 :(得分:2)
您可以使用$objectToArray
将第一个对象转换为数组,然后可以找到数组的$size
Show.aggregate([
{ "$addFields": {
"count": {
"$size": {
"$objectToArray": "$showTakenSeats"
}
}
}}
])
答案 1 :(得分:0)
我不知道聚合框架,但是一个简单的javascript解决方案应该是这样
T(1)={2,5,10}
T (3)={7,10}
T (4)={2,5,6}
T (6)={2,5}