在没有数组的情况下排除 Mongodb 中的嵌套子文档

clu*_*ter 4 mongodb meteor

以下是如何将数据插入到“产品”MongoDB 集合中(使用 Meteor):

Products.insert(
{
    productOne:
    {
        publicData:
        {
            pricePerUnit : 1,
            label : "The first product"
        },
        privateData:
        {
            test1: "xxxxx",
            test2: "xxxxx"
        }
    },
    productTwo:
    {
        publicData:
        {
            pricePerUnit : 2,
            label : "The second product"
        },
        privateData:
        {
            test1: "yyyyy",
            test2: "yyyyy"
        }
    }
}
);
Run Code Online (Sandbox Code Playgroud)

我想检索所有产品,但没有“privateData”子文档,以获取以下信息:

{
    productOne:
    {
        publicData:
        {
            pricePerUnit : 1,
            label : "The first product"
        }
    },
    productTwo:
    {
        publicData:
        {
            pricePerUnit : 2,
            label : "The second product"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我用“$elemMatch”尝试了几件事,但老实说我没有成功,我很难理解我应该怎么做。

有人会有建议吗?任何帮助将不胜感激。

谢谢!

gat*_*gaj 5

您的查询将类似于此

Products.find({},{
        fields: {
            privateData: 0
        }
    }
Run Code Online (Sandbox Code Playgroud)

privateData:0 将确保该字段被省略。

请参阅https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/了解更多信息