MongoDB find()的行为与嵌套文档的行为不符

时间:2018-08-27 07:37:23

标签: mongodb mongoose find pretty-print

这已经让我发疯了好几天了。

假定以下架构:

'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const dSchema = new Schema({
    dKey: {type: String, required:true},
    dProp1: {type: String},
    dProp2: {type: String},
})

const component_dModel = mongoose.model('d', dSchema);

const cSchema = new Schema({
    cKey: {type:String, required:true},
    cProp1: {type: String},
    cProp2: {type: String},
    d: [dSchema]
})

const component_cModel = mongoose.model('c', cSchema);

const bSchema = new Schema({
    bKey: {type:String, required:true},
    bProp1: {type: String},
    bProp2: {type: String},
    c: [cSchema]
})

const component_bModel = mongoose.model('b', bSchema);

const aSchema = new Schema({
    aKey: {type:String, required: true, unique: true},
    aProp1: {type: String},
    aProp2: {type: String},
    b: [bSchema]
})

module.exports = mongoose.model('test_databases',aSchema);

假设我将以下两个文档插入到我的收藏集中:

    {
    "aKey": "aKey1",
    "aProp1" : "value",
    "aProp2" : "value",
    "b" : [
        {
            "bKey": "bKey1",
        "c": [
                {
                    "cKey": "cKey1"
        },
        {
            "cKey": "ckey2"
        }
           ]
        },
    {
        "bKey" : "bKey2"
    }
    ]
}

和:

    {
    "aKey": "aKey2",
    "aProp1" : "value",
    "aProp2" : "value",
    "b" : [
        {
            "bKey": "bKey1",
        "c": [
                {
                    "cKey": "cKey1"
        },
        {
            "cKey": "ckey2"
        }
           ]
        },
    {
        "bKey" : "bKey2"
    },
    {
        "bKey" : "bKey3",
        "c": [
                {
                    "cKey": "cKey1"
        },
        {
            "cKey": "ckey2"
        }
           ]
        }
    ]
}

首先,我使用猫鼬,当我创建模式时,任何丢失的模式都会变成空数组,这很令人讨厌,但是我写了一些代码来清理它们(该代码在本测试代码中未使用,所以这就是为什么参见空的d数组)。我的查询没有解决我期望的问题。

我希望能够获取特定aKey文档及其下面的所有属性,或特定aKey文档的所有属性,以及特定bKey文档及其以下的属性,等等。

它适用于顶层:

db.test_databases.find({aKey: 'aKey1'})

仅返回akey aKey1文档。这就是我的期望

但是:

db.test_databases.find({aKey: 'aKey2', 'b.bKey':'bKey1', 'b.c.cKey':'cKey1'}).pretty()

我希望这将返回aKey中的属性,仅返回b / bKey1中的属性,并且仅返回b / c / cKey1中的属性,但是我会得到所有内容的合并(输出见下文)

如何构建查询,以便在上述示例中看不到b / bKey2或b / bKey3,也看不到b / c / cKey2,而仅看到b / c / cKey1?

这是我得到的输出:

{
        "_id" : ObjectId("5b83a409bd0fef17afa468e8"),
        "aKey" : "aKey2",
        "aProp1" : "value",
        "aProp2" : "value",
        "b" : [
                {
                        "_id" : ObjectId("5b83a409bd0fef17afa468ed"),
                        "bKey" : "bKey1",
                        "c" : [
                                {
                                        "_id" : ObjectId("5b83a409bd0fef17afa468ef"),
                                        "cKey" : "cKey1",
                                        "d" : [ ]
                                },
                                {
                                        "_id" : ObjectId("5b83a409bd0fef17afa468ee"),
                                        "cKey" : "ckey2",
                                        "d" : [ ]
                                }
                        ]
                },
                {
                        "_id" : ObjectId("5b83a409bd0fef17afa468ec"),
                        "bKey" : "bKey2",
                        "c" : [ ]
                },
                {
                        "_id" : ObjectId("5b83a409bd0fef17afa468e9"),
                        "bKey" : "bKey3",
                        "c" : [
                                {
                                        "_id" : ObjectId("5b83a409bd0fef17afa468eb"),
                                        "cKey" : "cKey1",
                                        "d" : [ ]
                                },
                                {
                                        "_id" : ObjectId("5b83a409bd0fef17afa468ea"),
                                        "cKey" : "ckey2",
                                        "d" : [ ]
                                }
                        ]
                }
        ],
        "__v" : 0
}

这是我想要的输出:

{
        "_id" : ObjectId("5b83a409bd0fef17afa468e8"),
        "aKey" : "aKey2",
        "aProp1" : "value",
        "aProp2" : "value",
        "b" : [
                {
                        "_id" : ObjectId("5b83a409bd0fef17afa468ed"),
                        "bKey" : "bKey1",
                        "c" : [
                                {
                                        "_id" : ObjectId("5b83a409bd0fef17afa468ef"),
                                        "cKey" : "cKey1",
                                        "d" : [ ]
                                },
                        ]
                },
        ],
        "__v" : 0
}

0 个答案:

没有答案