【问题标题】:Change root element of collection in MongoDB更改 MongoDB 中集合的根元素
【发布时间】:2018-07-13 20:47:33
【问题描述】:

我正在使用 MongoDB,并且有以下包含 500 个文档的数据。这是一个示例:

{ 
    "_id" : "17254", 
    "post_meta" : {
        "country" : "NZ", 
        "city" : "Christchurch", 
        "latlng" : {
            "lat" : -43.5320544, 
            "lng" : 172.6362254
        }, 
        "around" : "", 
        "likes" : NumberInt(0), 
        "type" : "", 
        "lang" : "de"
    }, 
    "post_blog_r" : DBRef("blogs", "3885", "bicore"), 
    "post_blog" : {
        "ref" : "blogs", 
        "id" : "3885", 
        "db" : "bicore"
}
{ 
    "_id" : "17256", 
    "post_meta" : {
        "country" : "NZ", 
        "city" : "Christchurch", 
        "latlng" : {
            "lat" : -43.5320544, 
            "lng" : 172.6362254
        }, 
        "around" : "", 
        "likes" : NumberInt(0), 
        "type" : "", 
        "lang" : "de"
    }, 
    "post_blog_r" : DBRef("blogs", "3885", "bicore"), 
    "post_blog" : {
        "ref" : "blogs", 
        "id" : "3885", 
        "db" : "bicore"
}
{ 
    "_id" : "17258", 
    "post_meta" : {
        "country" : "NZ", 
        "city" : "Lake Tekapo", 
        "latlng" : {
            "lat" : -44.0046736, 
            "lng" : 170.4771212
        }, 
        "around" : "", 
        "likes" : NumberInt(0), 
        "type" : "", 
        "lang" : "de"
}

我想在单独的列中获取仅包含经度 (lng) 和纬度 (lat) 的表格:

我设法像这样显示表格:

这是 JSON 视图中的内容:

{ 
    "_id" : "17254", 
    "post_meta" : {
        "latlng" : {
            "lat" : -43.5320544, 
            "lng" : 172.6362254
        }
    }
}
{ 
    "_id" : "17256", 
    "post_meta" : {
        "latlng" : {
            "lat" : -43.5320544, 
            "lng" : 172.6362254
        }
    }
}
{ 
    "_id" : "17258", 
    "post_meta" : {
        "latlng" : {
            "lat" : -44.0046736, 
            "lng" : 170.4771212
        }
    }
}

但是,我希望有两个单独的列,其中包含 lng 和 lat 信息,而没有 post_meta 级别。我尝试使用 $unwind 命令。不幸的是,它没有用。这就是我写的代码:

    use Neuseeland;
db.posts_nz_mongoexport.find({}, 
    { 
        "post_meta.latlng" : NumberInt(1)
    }
);

是否可以在单独的列中显示经度和纬度并将数据另存为新集合?输出应如下所示:

{ 
    "_id" : ObjectId("5b4908c71c93b41888ed83ab"), 
    "lat" : -43.5320544, 
    "lng" : 172.6362254
}
{ 
    "_id" : ObjectId("5b4908c71c93b41888ed83ac"), 
    "lat" : -43.5320544, 
    "lng" : 172.6362254
}
{ 
    "_id" : ObjectId("5b4908c71c93b41888ed83ad"), 
    "lat" : -44.0046736, 
    "lng" : 170.4771212
}

在此先感谢

【问题讨论】:

  • 如果你发布你的样本集合和输出而不是图像会很好,因为图像在这里不起作用......我知道你第二次发布了这个问题,所以试着按照我的建议做
  • 嗨安东尼,我已经更新了我的问题。感谢您指出这一点。

标签: arrays mongodb mongodb-query aggregation-framework


【解决方案1】:

您只需在此处$replaceRoot

db.collection.aggregate([
  {
    $addFields: {
      "post_meta.latlng.country": "$post_meta.country",
      "post_meta.latlng.city": "$post_meta.city"
    }
  },
  {
    $replaceRoot: {
      newRoot: "$post_meta.latlng"
    }
  }
])

输出

[
  {
    "lat": -43.5320544,
    "lng": 172.6362254
  },
  {
    "lat": -43.5320544,
    "lng": 172.6362254
  },
  {
    "lat": -44.0046736,
    "lng": 170.4771212
  }
]

查看here

【讨论】:

  • 太好了,成功了。非常感谢。我还有一个简短的问题。我将如何扩展代码以将 Country 和 City 列为列?
  • 更新了答案
【解决方案2】:

我认为最好的方法是使用 $replaceRoot$mergeObjects 运算符。

[
   {
      "$replaceRoot": {
         "newRoot": {
            "$mergeObjects": [
               "$post_meta.latlng",
               {
                  "country": "$post_meta.country",
                  "city": "$post_meta.city"
               }
            ]
         }
      }
   }
]

See demo here

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多