2014-02-12 20 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