2014-02-12 23 views
0

下面是我收集的文件MongoDB的錯誤

[0] => Array 
    (
     [Patent] => Array 
      (
       [allocation] => Array 
        (
         [1] => Array 
          (
           [r_1_a] => 17 
          ) 

        ) 

       [pn] => US20120101874A1 
       [id] => 52fb18775f44eaaf0a8b462a 
      ) 

    ) 

我試圖用$這個查詢放鬆操作:

db.patents.aggregate([{'$unwind':'$allocation'}]); 

這是給我的例外:

Error: Printing Stack Trace 
at printStackTrace (src/mongo/shell/utils.js:37:15) 
at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) 
at (shell):1:12 
Wed Feb 12 16:31:09.515 aggregate failed: { 
"errmsg" : "exception: $unwind: value at end of field path must be an array", 
"code" : 15978, 
"ok" : 0 
} at src/mongo/shell/collection.js:898 

幫我解決這個問題,因爲我在數組上應用$ unwind。

+1

如果你將文檔顯示爲由mongo shell代替而不是PHP轉儲,那麼這可能會更好。另外,你是否確定**所有**分配元素**都是**數組?這部分是有據可查的。 –

+0

'「分配」:{「1」:{「r_1_a」:17}},'。這裏是代表專利收集 – user199354

+0

'分配'不是給你添加在你的評論中的數組。這是一個名爲'「1」'的字段的子文檔。您的文檔保存代碼不會保存爲數組。 – WiredPrairie

回答

0

你必須放鬆每個嵌套陣列級別:

db.patents.aggregate([ 
    {'$unwind':'$Patent'}, 
    {'$unwind':'$Patent.allocation'} 
]); 

編輯:

看着你到目前爲止貼什麼,因爲錯誤的原因是,allocation對象,不是數組。最終這就是mongo告訴你的:

"errmsg" : "exception: $unwind: value at end of field path must be an array" 
+0

PHP Array的陣列。你確定? –

+0

嗨heinbo,它給我這樣的結果。 {「result」:[],「ok」:1} – user199354

+0

是尼爾這是一個數組類型字段,其集合名爲專利 – user199354