如何搜索 mongodb 中的嵌入文档?

How can I search for the embedded documents in mongodb?

我有一个collection这样的

{
    "_id" : ObjectId("5bbe1867839c0d2b4bdffcb2"),
    "name" : "Jyothish",
    "favBooks" : [
            {
                    "title" : "Let us C",
                    "author" : "Yaswanth Kanetkar",
                    "price" : 400
            },
            {
                    "title" : "Winner stands alone",
                    "author" : "Paulo Coelho",
                    "price" : 340
            }
    ]
}
{
    "_id" : ObjectId("5bbe1b62839c0d2b4bdffcb3"),
    "name" : "John",
    "favBooks" : [
            {
                    "title" : "Broken Republic",
                    "author" : "Arundhathi Roy",
                    "price" : 200
            },
            {
                    "title" : "One Life to Ride",
                    "author" : "Ajit Harisinghani",
                    "price" : 250
            }
    ]
}

有什么办法可以搜索到400以下的收藏夹吗?

我现在正在尝试的是

db.p.find({"favBooks.price":{$lt:400}}).pretty()

但这将 return 所有文档都有价格低于 400 的收藏夹商品。

我期望的输出是

{
        {
                "title" : "Winner stands alone",
                "author" : "Paulo Coelho",
                "price" : 340
        },
        {
                "title" : "Broken Republic",
                "author" : "Arundhathi Roy",
                "price" : 200
        },
        {
                "title" : "One Life to Ride",
                "author" : "Ajit Harisinghani",
                "price" : 250
        }
}

这可能吗?

提前致谢:)

您可以使用 $unwind and $replaceRoot

尝试以下聚合
db.collection.aggregate([
  { "$match": { "favBooks.price": { "$lt": 400 }}},
  { "$unwind": "$favBooks" },
  { "$match": { "favBooks.price": { "$lt": 400 }}},
  { "$replaceRoot": { "newRoot": "$favBooks" }}
])