0

我对 mongodb 很陌生,我遇到了一个简单(或不)的问题:

我有一个这样的标记产品集合:

{ "_id": "1", "tags": { "shape" : [ "shape1","shape2" ], "color": [ "col1","col2" ] },
{ "_id": "2", "tags": { "shape" : [ "shape3","shape4" ], "color": [ "col3","col4" ] },
{ "_id": "3", "tags": { "otherCategory" : [ "value1" ], "color": [ "col5","col6" ] }

我想从这个集合中选择所有不同的类别并得到这个结果:

[ "shape", "OtherCategory" ]

我怎样才能做到这一点?现在看来我唯一的选择是找到所有标签并在java客户端中手动解析它......似乎很糟糕;-)

我从 java spring 客户端查询 mongodb。

谢谢你的帮助。

4

1 回答 1

1

Ok so I ended up with refactoring my model: it looks like it's not easy to make queries on documents contents without doing some Map/reduce like Konstantin said.

To solve this problem I created a new collection of Categories so that I can easily select them. This collection is populated when the Products are insert.

于 2013-05-21T09:28:53.053 回答