2016-08-25 103 views
0

我有以下收集文件過濾掉從收集文檔字段並將其存儲在新的集合

{ 

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

我想打一個文件,如:

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

其中類型:Featuregeometry.type:Point是默認編寫的。座標是緯度和經度從「位置詳細信息」,進一步我想存儲這些生成的r將其存儲在另一個集合中。

+0

嗨Aditya - 你能解釋一下你想使用什麼樣的轉換,將這些經緯度數字轉換爲這些座標數字嗎? –

+0

試試這個帖子中的答案http://stackoverflow.com/questions/9711529/save-subset-of-mongodb-collection-to-another-collection –

回答

0

這下面的查詢和JavaScript函數將幫助您根據您的期望創建一個新的文檔。

執行在蒙戈查詢殼

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); 
}); 

你可以做一個查找執行上面的查詢

db.yournewcollectionname.find()之後,看到了新的集合中的所需文件。

樣本輸出

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

注:我在"Location Details"刪除的空間,把它作爲"LocationDetails"在查詢到達。處理沒有空格的關鍵名稱會更容易,這也是一個很好的做法。

另一種方法是,我們可以使用$aggregate$out一起獲得相同的結果。

請參閱這篇文章獲取更多信息Save Subset of MongoDB Collection to Another Collection

希望它能幫助!