当所有嵌入文档的属性均等于特定值时,猫鼬将获取所有文档,但当其具有另一个值时,猫鼬将忽略该属性

时间:2018-07-27 12:11:24

标签: 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" : "remove"
                }
        ],
        "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" : "buy"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "arrival"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "reserved"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "status" : "reserved",
}
{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "status" : "reserved",
}

我想在子文档的 all 具有等于status的{​​{1}}属性时恢复所有文档,但是如果它等于reserved没问题,所以当我运行查询时,我必须得到:

remove

我当前的查询是:

{
        "_id" : ObjectId("5b5852483a3e8a0d00bb7fef"),
        "items" : [
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff3"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff2"),
                        "status" : "remove"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff1"),
                        "status" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "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" : "reserved"
                },
                {
                        "_id" : ObjectId("5b5852483a3e8a0d00bb7ff0"),
                        "status" : "remove"
                }
        ],
        "status" : "reserved",
}

当所有嵌入文档的Order.find( { items: { $not: { $elemMatch: {status:{$ne: 'reserved' } }} } } ) 属性都等于status时检索文档。

任何帮助将不胜感激!

0 个答案:

没有答案