gpt4 book ai didi

javascript - 计算mongodb中对象内的键数

转载 作者:可可西里 更新时间:2023-11-01 09:38:30 26 4
gpt4 key购买 nike

<分区>

我想知道在一场演出中订购了多少个座位。

每个节目文档中都有一个“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。而不是应该不同的实际值。

count的结果应该是(3,3,0,0,0)

有人知道问题出在哪里吗?谢谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com