0

I have following collection document

 {

            "Dine" : {
                    "Cuisine" : "North Indian",
                    "Popular Dishes" : "Aaloo Tikki, Pao Bhaji, Bhalla Papdi",
                    "Dine In Available" : "Yes",
                    "Online Booking" : "",
                    "Home Delivery" : "",
                    "Pure Veg" : "Yes",
                    "Restaurant Type" : "",
                    "Air Conditioned" : "",
                    "Outdoor Area" : "",
                    "Pint of Beer" : "",
                    "Bar Available" : "",
                    "Wifi" : "",
                    "Number of Deals" : "",
                    "Happy Hours" : "",
                    "Cost for 2" : "Rs 0-250",
                    "Credit Cards Accepted" : "",
                    "Deal Available" : ""
            },
            "Location Details" : {


                    "Latitude" : "28.7004740",
                    "Longitude" : "77.1174010",

            },
            "MetaData" : {
                    "Meta Description" : "",
                    "Meta Keywords" : ""
            },
            "Operation Hours" : {
                    "Operation Hours" : "11 AM-11 PM"
            },
            "Other Options" : {
                    "Chain Name" : "",
                    "Expiry Date" : "",
                    "As seen in" : "",
                    "Deals Head" : "",
                    "Deals ID" : "",
                    "Events Head" : "",
                    "Events ID" : "",
                    "Show Bookings" : "",
                    "Form Reciever Email" : "",
                    "Powered By" : "",
                    "Show Photos" : ""
            },
            "__v" : 0,
            "_id" : "-bittoo"
    }

I want to make a document like:

  {
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  }
}

where type: Feature and geometry.type: Point are written by default.Also the coordinates are latitude and longitude from"Location Details",further I want to store these generated result to get stored in another collection.

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • Hi Aditya - can you explain what transformation you want to use, to convert those latitude and longitude numbers to those co-ordinate numbers? – Vince Bowdren Aug 25 '16 at 11:03
  • Try the answers in this post http://stackoverflow.com/questions/9711529/save-subset-of-mongodb-collection-to-another-collection – Clement Amarnath Aug 25 '16 at 11:49

1 Answers1

0

This below query and javascript function will help you in creating a new document as per your expectation.

Execute the query in mongo shell

db.yourcollectionname.find().forEach(function(doc){
   var array = [doc.LocationDetails.Latitude, doc.LocationDetails.Longitude];
   var geometry = { "type" : "point",  "coordinates" : array};
   var newDoc = {};
   newDoc["type"] = "Feature";
   newDoc["geometry"] = geometry;

   db.yournewcollectionname.insert(newDoc);
});

You can do a find after executing the above query

db.yournewcollectionname.find() and see the desired documents in the new collection.

A sample output

{
        "_id" : ObjectId("57bee0cbc9735bf0b80c23e0"),
        "type" : "Feature",
        "geometry" : {
                "type" : "point",
                "coordinates" : [
                        "28.7004740",
                        "77.1174010"
                ]
        }
}

Note: I have removed the space in "Location Details" and had it as "LocationDetails" for arriving at the query. It would be easier to process key names without spaces and it is also a good practice.

An alternative approach to this is we can use $aggregate and $out together to acheive the same result.

Refer this post for more info Save Subset of MongoDB Collection to Another Collection

Hope it Helps!

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34