2014-02-05 220 views
0

H有帶數組的json文檔。因爲我不能用push或addtoset添加到數組的開頭,所以我需要對數組進行排序。示例在Mongo中對數組進行排序

{ 
    "Component": [ 
    { 
     "Id": "PDP-1", 
     "Links": {"Link": [ 
      { 
      "Text": "Western Division", 
      "Url": "/1x7-en70ai/last-minute-holidays-western-division", 
      "Title": "Last minute holidays Western Division" 
      }, 
      { 
      "Text": "Browse Regions ", 
      "Url": "/1x7-en6uly-10ts/last-minute-holidays-gambia/regions", 
      "Title": "Last minute holidays Gambia", 
      "Style": "BrowseForMore" 
      }, 
      { 
      "Text": "City of Banjul", 
      "Url": "/1x6-en6vq7/holidays-city-of-banjul", 
      "Title": "City of Banjul Holidays" 
      }, 
      { 
      "Text": "Western Division", 
      "Url": "/1x6-en70ai/holidays-western-division", 
      "Title": "Western Division Holidays" 
      } 
     ]}, 
     "Title": "Regions", 
     "Type": "PDP" 
    }, 
    { 
     "Id": "PDP-2", 
     "Links": {"Link": [ 
      { 
      "Text": "Bijilo", 
      "Url": "/1x7-enbmy6/last-minute-holidays-bijilo", 
      "Title": "Last minute holidays Bijilo" 
      }, 
      { 
      "Text": "Browse Cities ", 
      "Url": "/1x7-en6uly-10tt/last-minute-holidays-gambia/cities", 
      "Title": "Last minute holidays Gambia", 
      "Style": "BrowseForMore" 
      }, 
      { 
      "Text": "Banjul Beach", 
      "Url": "/1x6-enakgm/holidays-banjul-beach", 
      "Title": "Banjul Beach Holidays" 
      }, 
      { 
      "Text": "Bijilo", 
      "Url": "/1x6-enbmy6/holidays-bijilo", 
      "Title": "Bijilo Holidays" 
      }, 
      { 
      "Text": "Brufut Heights", 
      "Url": "/1x6-encok8/holidays-brufut-heights", 
      "Title": "Brufut Heights Holidays" 
      }, 
      { 
      "Text": "Kololi", 
      "Url": "/1x6-enpnle/holidays-kololi", 
      "Title": "Kololi Holidays" 
      }, 
      { 
      "Text": "Kotu", 
      "Url": "/1x6-enq067/holidays-kotu", 
      "Title": "Kotu Holidays" 
      } 
     ]}, 
     "Title": "Cities", 
     "Type": "PDP" 
    } 
    ], 
    "Id": "118431", 
    "Template": { 
    "PageTemplate": { 
     "Code": "2B2", 
     "text": "041 - TEMP2 - COP_CONCOU_{LAST MINUTE}" 
    }, 
    "Category": { 
     "Code": "1X7", 
     "Type": "Product", 
     "text": "Last minute holidays" 
    }, 
    "GeoObject": { 
     "Code": "EN6ULY", 
     "text": "Gambia, The" 
    }, 
    "GeoObjectType": { 
     "Code": "1A", 
     "text": "Political" 
    }, 
    "GeoObjectSubType": { 
     "Code": "10TR", 
     "text": "Country" 
    } 
    }, 
    "Type": "Content", 
    "Url": "/1x7-en6uly/last-minute-holidays-gambia", 
    "_id": {"$oid": "528492d4c90fa9fcd0436929"} 
} 

我想在Links.Link'BrowseForMore'中按Style排序。任何想法如何做到這一點?我想我可以通過push添加虛擬數組,然後按照我想要的方式進行分類。任何幫助表示讚賞

+2

如果您對代碼進行格式化,則回答起來會更容易。格式化爲 –

+0

。希望更好地獲得 – PaulManningBiz

+0

你是什麼意思「按Links.Link'BrowseForMore'中的樣式排序」? – JohnnyHK

回答

1

您似乎想要更新數組並保持排序順序與列表前面的Links.Link.Style值。在這種情況下,使用帶更新的$sort修飾符。

db.collection.update(
    { _id: id }, 
    { $push: { "Links.Link: {$each: [doc], $sort { Style: -1 }}} } 
) 

$each要求操作員即使只有一個文件,但可以採取很多。

如果您嘗試使用$addToSet來維護唯一的文檔,官方的MongoDB行是被認爲是無序的,因此修飾符在這裏不可用。

+0

謝謝。我不知道如何在你的示例中替換[doc],因爲有三個或四個字段,例如文本,網址,標題和樣式。因此不應該是: – PaulManningBiz

+0

db.collection.update( {_id:id}, {$ push:{「Links」:{$ each:[Link],$ sort {Style:-1}}}} ) – PaulManningBiz

+0

在我的答案中,「可以帶很多」,每個參數都是一個列表,在[]之間用逗號分隔值或變量(分別用'id'和'doc'的變量的佔位符)。回答中的$ each操作符上的鏈接具有所有文檔 –