我正在关注一个画廊的文件(Json),
{ "_id": "53698b6092x3875407fefe7c", "status": "active", "colors": [ "red", "green" ], "paintings": [ { "name": "MonaLisa", "by": "LeonardodaVinci" }, { "name": "JungleArc", "by": "RayBurggraf" } ] }
现在我也有一组颜色说
颜色 - 收集:[“黑色”,“黄色”,“红色”,“绿色”,“蓝色”,“粉红色”]
我希望通过它的名称来匹配提取的文字说明“MonaLisa”(作为搜索查询)我还想比较两种颜色和COLORS-COLLECTION,如果颜色在COLORS中有任何匹配的颜色-COLLECTION然后它应该返回绘画。
我想要类似下面的东西:
{ "paintings": [ { "name": "MonaLisa", "by": "LeonardodaVinci" } ] }
请帮帮我!!提前谢谢。
答案 0 :(得分:1)
如果我找到你,aggregation framework会做你的工作:
db.gallery.aggregate([
{"$unwind": "$paintings"},
{"$match": {"paintings.name": 'MonaLisa', "colors": {"$in": ["black","yellow","red","green","blue","pink"]}}},
{"$project": {"paintings": 1, "_id": 0}}
]);