当所有嵌入/嵌套文档的属性都等于

时间:2018-07-26 22:01:28

标签: database mongodb mongoose

我有以下Order模型集合:

{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff2"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff1"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff2"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff1"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "buy"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}

我要检索所有所有个嵌入文档(项目)的状态为reserved的文档,因此当我运行查询时,我必须得到:

{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}

当我使用以下查询时,我得到的所有文档在其嵌套文档中甚至包含一个等于reserved的状态

var orders = await Order.find({"items.status": "reserved"}).exec()

任何帮助将不胜感激!

0 个答案:

没有答案