使用$ unwind获取空数组作为聚合结果

编程入门 行业动态 更新时间:2024-10-24 08:29:22
本文介绍了使用$ unwind获取空数组作为聚合结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个如下文档,使用猫鼬应用了聚合查询,该查询粘贴在json数据之后.在这里,我尝试使用子收集时间字段.查询结果为空数组,请帮助查找错误.查询可删除$unwind和相关的$project字段

I have a document like below, applied an aggregate query using mongoose which is pasted after json data. Here I am trying to use fields of sub collection- timing. Query results an empty array, please help to find mistake in query. Query works on removing $unwind and related $project field

{ "_id":"5b9cbeff67ea7bc00c9917a5", "route_index":0, "route_name":"NH85", "access_type":"public", "type":"bus", "duration_days":null, "duration_hours":0.23, "created_on":"2018-09-15T07:50:17.211Z", "title":"Rx100", "description":"Ls", "timings":[ { "departure":"08:00", "destination":"08:23", "_id":"5b9cbeff67ea7bc00c9917a3", "trackings":[ ] }, { "departure":"10:00", "destination":"10:23", "_id":"5b9cbeff67ea7bc00c9917a2", "trackings":[ ] } ], "created_uid":"5b8f1a9db83bc9bf3d89dcc5", "__v":0 }

我尝试过的查询在这里:

Query which I have tried is here:

Transport.aggregate([ { $unwind: "$timings" } { $project: { title: 1, "trip_timings": "$timings" } }, { $skip: ((perPage * page) - perPage) }, { $limit: perPage } ], function (err, data) { if (err) { res.json(err); } res.json(data); });

推荐答案

尝试在$ unwind操作中将preserveNullAndEmptyArrays设置为true.可能是由于"trip_timings"字段包含空数组而导致结果被跳过. $ unwind的默认行为是忽略要取消展开的字段不存在或为空数组的文档.此处提供更多详细信息: docs.mongodb/manual /reference/operator/aggregation/unwind/#behaviors

Try setting preserveNullAndEmptyArrays to true in the $unwind operation. It might be that the results are being skipped because the "trip_timings" field contains empty arrays. The default behaviour of $unwind is to ignore documents where the field to be unwound does not exist or is an empty array. More details here: docs.mongodb/manual/reference/operator/aggregation/unwind/#behaviors

更多推荐

使用$ unwind获取空数组作为聚合结果

本文发布于:2023-11-22 18:40:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   unwind

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!