mongodb - 带有 $push 的 mongo 组不允许重复键
问题描述
我有以下文件结构:
{
readings:[
{"timestamp":"1234","data":"9"},
{"timestamp":"1234","data":"90"},
{"timestamp":"12" "data":"100"}]
}
是否可以使用重复键对三个数组进行分组。
预期结果 :
{
"readings":["1234","1234","12"]
}
我的查询:
[
{
$group: {
_id: "null",
readings: {
$push: "$readings.timestamp"
}
}
}
];
但它不允许重复键。
解决方案
readings
是一个数组,当你分组时,你会得到数组的数组。如果您readings
将字段展平并对其进行分组,您将获得所需的结果。
试试这个:
db.collection.aggregate([
{
$unwind: "$readings"
},
{
$group: {
_id: "null",
readings: {
$push: "$readings.timestamp"
}
}
}
])
推荐阅读
- javascript - curl -H "Content-Type: application/json" "localhost:1337/webhook" 不工作
- angularjs - 如何为 ui-select 提供一个不变的选择?
- amazon-s3 - 谷歌云数据传输到 GCS 子文件夹
- angularjs - 如何使用 VAST 标签在 ima3 插件的非精简广告中显示关闭按钮?
- regex - 以任意顺序匹配部分和整个单词
- java - 刽子手新游戏如何重置内存?
- node.js - 当一个请求的回复依赖于另一个请求的回复时,如何实现?
- php - 如何在 PHP 变量中存储加号?
- php - 如果生日比照片其他没有生日
- html - 如何将 html 电子邮件代码模板添加到 javascript 以使用 googlesheet 发送个性化邮件