Mongodb query on nested array elements

后端 未结 1 1504
有刺的猬
有刺的猬 2020-12-11 14:15

for my new project we started to use mongodb as a document repository. It works great for our requirements but now we need to implement a more advanced query on sub-sub-arr

相关标签:
1条回答
  • 2020-12-11 14:30

    Since there is no way to tell what driver you use here is a shell solution:

    db.foo.find({
        "Asset.Metadata.Platforms.Platform": {
            $elemMatch: {
                "@name": VAR_PLATFORM,
                "PublishingRanges.PublishingRange": {
                    $elemMatch: {
                        "@startdate": {$gt: VAR_START},
                        "@enddate": {$lt: VAR_END}
                    }
                }
            }
        }  
    })
    

    By the way, you could simplify document structure by omitting Platform and PublishingRange and assigning arrays to Platforms an PublishingRanges respectively.

    0 讨论(0)
提交回复
热议问题