使用 mongodb 和嵌入式文档集合来应对令人敬畏的查询构建器限制

问题描述

我有 2 个集合,Authors 和 Books,我想使用 react-awesome-query-builder 使用户能够以他们喜欢的方式查询数据,这是我的架构和数据

作者收藏

{id: 1,name: 'JK Rowling',gender: 'F'},{id: 2,name: 'Roald Dahl',gender: 'M'},{id: 3,name: 'Dr Seuss',gender: 'M'}

图书收藏

{id: 1,author_id: 1,title: 'Harry Potter and the Philosophers Stone',year: 1997},title: 'Harry Potter and the Goblet of Fire',year: 2000},author_id: 2,title: 'Charlie and the Chocolate Factory',year: 1964},{id: 4,title: 'James and the giant peach',year: 1961},{id: 5,author_id: 3,title: 'Green eggs and ham',year: 1960},{id: 6,title: 'The cat in the hat',year: 1957}

我希望能够构建查询以“向我展示 1960 年代出版书籍的所有作者”,并且它返回 mongo 查询允许我进行查询,该查询在执行时返回以下结果:

{id: 2,name: 'Roald Dahl'},name: 'Dr Seuss'}

我不知道如何安排我的数据、一个“作者”集合与嵌入的“书籍”文档数组、两个单独的集合或两者的组合?

Mongo 可以很好地处理嵌入式文档,但不确定 react-awesome-query-builder 的功能

解决方法

似乎 $elemmatch 支持已添加到 react-awesome-query-builder 中,所以我只需要将我的 !struct 配置更改为 !group 和瞧!