在MongoDB中更改集合的根元素

时间:2018-07-13 20:47:34

标签: arrays mongodb mongodb-query aggregation-framework

我正在使用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
}

非常感谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

See demo here