mongodb - Mongodb / mongoose聚合 - 计算嵌套文档中字段的字符串出现次数它返回具有相同名称的_id - 计数是正确的
问题描述
我正在尝试计算我的 MongoDB 集合中名为 Events 的嵌套(子)文档中不同字符串出现的次数。
第一的:
- 它使用 $match 查找特定的事件 ID
- 然后它 $unwinds 名为 requestList 的嵌套文档
- 然后它 $unwinds 请求列表中名为 songStatus 的字段
- 然后它 $group 并计算 ($sum) 每个不同的事件
对于我的文档,它应该返回一个名为 data 的数组,其中包含 3 个对象: data: Array(3)
- 0:{_id:“generalRequestQueue”,计数:5}
- 1:{_id:“playNowQueue”,计数:3}
- 2:{_id:“队列”,计数:1}
相反,它返回数据:Array(3):
- 0:{_id:“队列”,计数:5}
- 1:{_id:“队列”,计数:3}
- 2:{_id:“队列”,计数:1}
当文档中的 songStatus 显然有 3 个不同的值时,为什么它们都被称为队列?计数是正确的,_ids 不是。
这是我的代码:
countSongStatuses: function (req, res) {
db.Event.aggregate([
// Limit matching documents (can take advantage of index)
{
$match: {
"_id": req.params.id
}
},
// Unpack the requestList and songStatuses
{ "$unwind": "$requestList" },
{ "$unwind": "$requestList.songStatus" },
// Group by the answer values
{
"$group": {
"_id": "$requestList.songStatus",
count: { $sum: 1 }
}
}])
.then((dbModel) => res.json(dbModel))
.catch((err) => res.status(422).json(err));
},
这是我的文件:
{
"_id" : "55f536a-2186-56d-4e23-54dd657178",
"subIdForEventStatusChange" : "55f536",
"eventStatus" : "activated",
"genre" : "Everything!",
"eventDate" : ISODate("2021-05-05T00:00:00.000Z"),
"startTime" : "5:59pm",
"endTime" : "9:00pm",
"eventName" : "Cinco de Mayo Celebration",
"eventType" : "Dinner Dance Party",
"venueName" : "Casa de Pico",
"venueAddress" : "1234 La Mesa Blvd.; La Mesa, CA 91942",
"eventImage" : "https://res.cloudinary.com/noimgmt/image/upload/v1615839494/noireqapp/yd5bfcxh7cw5e2fg2wof.jpg",
"requestList" : [
{
"_id" : ObjectId("604fc1504c10105a54ae2a78"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/fc2eb7a560c2a46acbb17362bb373c59.png",
"customerName" : "Larisse Robinson",
"title" : "Firework",
"artist" : "Katy Perry",
"generalRequest" : false,
"playNow" : true,
"tip" : 100,
"songStatus" : "playNowQueue"
},
{
"_id" : ObjectId("604fc1ad4c10105a54ae2a79"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/a1ef00507ce94ac6b59d00d6603a1a22.png",
"customerName" : "Charles Robinson",
"title" : "Around the World",
"artist" : "Red Hot Chili Peppers",
"generalRequest" : false,
"playNow" : true,
"tip" : 25,
"songStatus" : "queue"
},
{
"_id" : ObjectId("604fc1e04c10105a54ae2a7a"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/e06f235d701ef090e8a76e81780c1965.png",
"customerName" : "Charles Robinson",
"title" : "Bat Out of Hell",
"artist" : "Meatloaf",
"generalRequest" : false,
"playNow" : true,
"tip" : 20,
"songStatus" : "playNowQueue"
},
{
"_id" : ObjectId("60510eae71340e09e8a15b69"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c56b06a92b944251b80fc8f4ee32c3fb.png",
"customerName" : "Charles Robinson",
"title" : "Everyday",
"artist" : "Dave Matthews Band",
"generalRequest" : false,
"playNow" : true,
"tip" : 25,
"songStatus" : "playNowQueue"
},
{
"_id" : ObjectId("6051727ffa40160a80fac1e7"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/01bccad9d7be4980c9fdbcec3be695c9.png",
"customerName" : "Larisse Robinson",
"title" : "Best Day of My Life",
"artist" : "American Authors",
"generalRequest" : true,
"playNow" : false,
"tip" : 25,
"songStatus" : "generalRequestQueue"
},
{
"_id" : ObjectId("605172c4fa40160a80fac1e8"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/b0c33f40f31b63909c93f587824347b2.png",
"customerName" : "Charles Robinson",
"title" : "Brick House",
"artist" : "Commodores",
"generalRequest" : true,
"playNow" : false,
"tip" : 20,
"songStatus" : "generalRequestQueue"
},
{
"_id" : ObjectId("605173ced9514e635c806bf9"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/92f33281764af1c194e83a6fbfa3552c.png",
"customerName" : "Charles Robinson",
"title" : "Friends In Low Places",
"artist" : "Garth Brooks",
"generalRequest" : true,
"playNow" : false,
"tip" : 15,
"songStatus" : "generalRequestQueue"
},
{
"_id" : ObjectId("60517401d9514e635c806bfa"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/c4b859f726d6ff189fe3ec68a4156dd4.png",
"customerName" : "Charles Robinson",
"title" : "Low",
"artist" : "Flo Rida",
"generalRequest" : true,
"playNow" : false,
"tip" : 15,
"songStatus" : "generalRequestQueue"
},
{
"_id" : ObjectId("6052740ad19a1b186035569a"),
"albumCover" : "https://lastfm.freetls.fastly.net/i/u/174s/f46fad9ede1dc6bd1e653f0fe79e6fc2.png",
"customerName" : "Charles Robinson",
"title" : "Sexy And I Know It",
"artist" : "LMFAO",
"generalRequest" : true,
"playNow" : false,
"tip" : 25,
"songStatus" : "generalRequestQueue"
}
],
"__v" : 0
}
解决方案
问题解决了!Mongodb 显然不喜欢我的大杂烩 requestList,因为我更新了模型并且没有删除数据库。我只是手动添加了字段。我删除了数据库,它按设计工作!那里吸取了教训!
推荐阅读
- javascript - 如何修复模态弹出关闭按钮在 Wordpress 上的显示部分?
- angular - 2 方式绑定 ngModel 更改也反映在其分配的临时变量中(在页面加载时分配)
- c# - 我可以像这样从两个 texbox 中获得不匹配的值吗?
- javascript - jQuery 如何为每个单独的行更改行背景颜色?
- spring-boot - 如何在 Cassandra 中存储自定义对象?
- c# - 如何使用 double 方法返回两个列表?
- javascript - 哪个 HTML 元素用于设置最大行长以及最大和最大行数?
- java - 如何在 firebase 数据库中获取特定键并比较它们是否相同?
- raku - 每当信号()在反应块顺序依赖?
- python - PermissionError: [WinError 32] for GridSearchCV in spyder