2013-02-27 144 views
0

時放鬆身心的錯誤我有一個文件結構如下:

{ 
    "_id" : NumberLong("80000000012"), 
[...] 
    "categories" : [{ 
     "parent" : "MANUFACTURER", 
     "category" : "Chevrolet" 
    }, { 
     "parent" : "MISCELLANEOUS", 
     "category" : "Miscellaneous" 
    }], 
[...] 
} 

我試圖讓所有的「類別」字段的不同列表中爲每個「父」場。我試圖利用聚合框架用下面的查詢做到這一點:

db.posts_temp.aggregate(
    {$unwind : '$categories'}, 
    {$match : {'categories.parent' : 'MISCELLANEOUS'}}, 
    {$project : { 
     '_id' : 0, 
     parent : '$categories.parent', 
     category : '$categories.category' 
     } 
    }, 
    { 
     $group : { 
      _id : '$parent', 
      category : {$addToSet : '$category'} 
     } 
    } 
); 

運行該查詢返回以下錯誤:

{ 
    "errmsg" : "exception: $unwind: value at end of field path must be an array", 
    "code" : 15978, 
    "ok" : 0 
} 

這似乎被捆綁到查詢的組部分,因爲當我刪除它時,查詢運行正常,但顯然,數據並不是我想要的。

回答

0

我只是嘗試在我的mongo實例上執行上面的聚合查詢。這裏是我的3個文檔,每個文檔都有一個包含兩個嵌套文檔數組的鍵。

這裏是我的數據:

{ 
    "_id" : ObjectId("512d5252b748191fefbd4698"), 
    "categories" : [ 
     { 
      "parent" : "MANUFACTURER", 
      "category" : "Chevrolet" 
     }, 
     { 
      "parent" : "MISCELLANEOUS", 
      "category" : "Miscellaneous" 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("512d535cb748191fefbd4699"), 
    "categories" : [ 
     { 
      "parent" : "MANUFACTURER", 
      "category" : "Chevrolet" 
     }, 
     { 
      "parent" : "MISCELLANEOUS", 
      "category" : "Pickup" 
     } 
    ] 
} 
{ 
    "_id" : ObjectId("512d536eb748191fefbd469a"), 
    "categories" : [ 
     { 
      "parent" : "MANUFACTURER", 
      "category" : "Toyota" 
     }, 
     { 
      "parent" : "MISCELLANEOUS", 
      "category" : "Miscellaneous" 
     } 
    ] 
} 

這裏是你的聚集查詢我跑:

db.posts_temp.aggregate({$unwind:'$categories'} , {$match: {'categories.parent':'MISCELLANEOUS'}}, {$project:{'_id':0, parent: '$categories.parent', category:'$categories.category'}}, {$group:{_id:'$parent', category:{$addToSet:'$category'}}}) 

下面是結果:

{ 
    "result" : [ 
     { 
      "_id" : "MISCELLANEOUS", 
      "category" : [ 
       "Pickup", 
       "Miscellaneous" 
      ] 
     } 
    ], 
    "ok" : 1 
} 

讓我知道如果我的數據和你的數據有一些差異。

CSharpie

+0

我們在工作中再次運行此操作,並在展開之前移動了匹配語句並修復了它。我們最好的猜測是,放鬆創建一些內存集合,這是太大,並拋出一個錯誤。我們有約18K的文件在收藏。 *聳聳肩* – ryan1234 2013-02-27 05:09:16

+0

^他說什麼。感謝您的期待。 – kerpazm 2013-02-27 23:16:03