jeem67
jeem67

Reputation: 29

Change root element of collection in MongoDB

I am using MongoDB and have the following data with 500 documents. Here is a sample:

{ 
    "_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"
}

I would like to get a table only with longitude (lng) and latitude (lat) in separate columns like that:

I managed to display the table like this:

This is it in the JSON View:

{ 
    "_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
        }
    }
}

However, I would like to have two separate columns with the information lng and lat without the post_meta level. I tried to work with the order $unwind. Unfortunately, it didn't work. Thats the code, I have written:

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

Is ist possible to show longitude and latitude in separate columns and save the data as new collection? The Output should look like this:

{ 
    "_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
}

Many thanks in advance

Upvotes: 1

Views: 411

Answers (2)

Sede
Sede

Reputation: 61225

I think the best way to do this is with $replaceRoot and the $mergeObjects operators.

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

See demo here

Upvotes: 1

Ashh
Ashh

Reputation: 46451

You need to $replaceRoot here only

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

Output

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

Check it here

Upvotes: 1

Related Questions