尝试使用C# / Linq或原始的Mongodb查询本身来理解如何将多个数组作为笛卡尔积连接。例如,假设我有一个集合,我将其过滤为以下两个文档:
如何将每个数组中的每个项连接起来以产生以下类型的结果?
我正在寻找一种解决方案,可以适用于任意数量的文档。例如,在这个例子中,如果有第三个文档也有3个对象数组,那么会产生一个包含27个项目的结果集 - 或者说27行。
[
{"movie":"starwars","showday":"monday"},
{"movie":"batman","showday":"thursday"},
{"movie":"sleepless","showday":"tuesday"}
]
[
{"actor":"angelina","location":"new york"},
{"actor":"jamie","location":"california"},
{"actor":"mcavoy","location":"arizona"}
]
如何将每个数组中的每个项连接起来以产生以下类型的结果?
[{"movie":"starwars","showday":"monday","actor":"angelina","location":"new york"},
{"movie":"batman","showday":"thursday","actor":"angelina","location":"new york"},
{"movie":"sleepless","showday":"tuesday","actor":"angelina","location":"new york"},
{"movie":"starwars","showday":"monday","actor":"jamie","location":"california"},
{"movie":"batman","showday":"thursday","actor":"jamie","location":"california"},
{"movie":"sleepless","showday":"tuesday","actor":"jamie","location":"california"},
{"movie":"starwars","showday":"monday","actor":"mcavoy","location":"arizona"},
{"movie":"batman","showday":"thursday","actor":"mcavoy","location":"arizona"},
{"movie":"sleepless","showday":"tuesday","actor":"mcavoy","location":"arizona"}]
我正在寻找一种解决方案,可以适用于任意数量的文档。例如,在这个例子中,如果有第三个文档也有3个对象数组,那么会产生一个包含27个项目的结果集 - 或者说27行。
希望找到如何使用C#(Linq?)Mongodb驱动程序查询和返回此类数据的解决方案,但即使是mongodb特定的查询,我也会尝试从中反向推理出逻辑。谢谢
{[电影],[演员]}
而不是电影{电影,上映日期,[演员]}
?请原谅这个话题有些偏离。 - Munzer$lookup
等类似操作,但这些操作需要有关键字。你需要在代码中完成这个任务,或者使用.eval()
在服务器上运行代码。但不要使用eval()
。因此,这是一个编程练习而不是数据库解决方案。 - Neil Lunn