简体   繁体   English

MongoDB (Mongoose) 按数组查询

[英]MongoDB (Mongoose) Querying By Array

Document Example:文档示例:

{
    "_id" : ObjectId("5e41b8ecb6d9d829d07d3c5c"),
    "id" : "foo-bar-1",
    "__v" : 0,
    "books" : [ 
        {
            "_id" : ObjectId("5e41c81dbaa2b215082b52a1"),
            "chapters" : [ 
                {
                    "_id" : ObjectId("5e41c81dbaa2b215082b52a2"),
                    "title" : "Foo",
                    "pages" : 20
                },
                {
                    "_id" : ObjectId("5e41c81dbaa2b215082b52a3"),
                    "title" : "Bar",
                    "pages" : 34
                }
            ]
        }
    ],
    "last_updated" : ISODate("2020-02-10T15:10:50.027-05:00")
    }

Hi,你好,

I would like to perform a MongoDB Query where I find the books with the exact same chapters.我想执行 MongoDB 查询,在其中找到具有完全相同章节的books Ideally, I would like to perform something like this:理想情况下,我想执行以下操作:

 const chapters = [{title: 'Foo', pages: 20},{title: 'Bar', pages: 34}];
 const book = await Model.findOne({'books.chapters': chapters});

However, from my testing this only works when the chapters array includes the _id fields.但是,根据我的测试,这仅在chapters数组包含_id字段时才有效。 In the case I would like to use this I would not know what the subdocument schemas' id are.在我想使用它的情况下,我不知道子文档模式的 id 是什么。 Is there a way to ignore them when comparing arrays and/or is there a better way to do this comparison.在比较数组和/或是否有更好的方法来进行这种比较时,有没有办法忽略它们。

Thank you for your time.感谢您的时间。

to match identical array fields the arrays must be identical in all ways...the fields, their order, and their values...title, pages, object id, etc要匹配相同的数组字段,数组必须在所有方面都相同……字段、它们的顺序和它们的值……标题、页面、对象 ID 等

working with the current schema then you would need to filter out just the title (page count too?) via an aggregation pipeline query's stage and then use an intersect to establish the match...使用当前模式,那么您需要通过聚合管道查询的阶段仅过滤掉标题(页数?),然后使用相交来建立匹配...

depending on where you are in your development - if still early you could create a new array field of just chapterNames during the data input process...in addition to your existing array field Chapters...if that would suffice not sure if the page count is needed or not取决于您在开发中的位置 - 如果还早的话,您可以在数据输入过程中创建一个只有 ChapterNames 的新数组字段......除了现有的数组字段 Chapters ......如果这足以确定页面计数是否需要

still in this potential new field chapterTitles , as an array, to match the chapters must be in an identical order, identical spelling..仍然在这个潜在的新字段中 ChapterTitles ,作为一个数组,匹配章节必须以相同的顺序,相同的拼写..

if you don't require that all chapters match - but are only looking for individual chapter name matches - then in the current schema you would set up a $unwind to separate apart the array into individual elements for matching purposes...this is also done in an aggregate pipeline stage...如果您不要求所有章节都匹配 - 而只是寻找单个章节名称匹配 - 那么在当前模式中,您将设置一个 $unwind 以将数组分隔为单个元素以进行匹配......这也是在聚合管道阶段完成...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM