无法获取 mongodb 查询中未知键的数据
我想使用 mongodb 查询从文档中获取子数组,但我不知道它的键。我的数据看起来像这样:
{ 'log file name I don't Know': [Array] }
'log file name I don't Know' 是该级别的唯一键。 mongodb 聚合管道中是否有类似“获取第一个键的值”之类的内容?
I want to get a subarray from a document using mongodb queries but I don't know the key for it. My data looks something like this:
{ 'log file name I dont know': [Array] }
'log file name I dont know' is the unique key at that level. Is there something like "get value for the first key" in mongodb aggregation pipeline?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1 也许是这样的:
解释:
地图.k ->未知的钥匙
地图.v ->未知键的数组值
最后,您可以在 $match 阶段添加搜索到的元素,在示例中:"map.v":4
playground1
2 当你想在比赛阶段后保留未知键的奇怪结构时,可以选择以下选项:
解释:
playground2
几句话:文件名值不建议作为键,最佳实践是文件名是键/值,因此如果您想搜索文件名值,则可以在该文件名键字段上创建索引,因此您的 json 结构如下所示:
1 Maybe something like this:
Explained:
map.k -> the unknown keys
map.v -> the array values for the unknown keys
Finally you can add in the $match stage the searched element , in the example:"map.v":4
playground1
2 Here is the option when you want to preserve the strange structure with the unknown key after the match stage:
Explained:
playground2
Few words: File name value is not recommended to be a key , best practice is the filename to be key/value so if you want to search on filename value to be possible to create index on that filename key field , so your json structure to looke something like: