mongodb - 如何使用 Autopopulate Mongoose Mongodb 选择要检索的字段?
问题描述
我正在使用插件 mongoose-autopopulate 但我不知道如何指定要从填充集合中检索的字段。
这是我的架构...
const categoriaSchema = new Schema({
data: { type: String },
label: { type: String },
children: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'categorias',
autopopulate: true
}],
father: {
type: mongoose.Schema.Types.ObjectId,
ref: 'categorias',
autopopulate: true
},
eventos: { type: Boolean },
tienda: { type: Boolean },
productos: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'productos',
autopopulate: true
}]
})
所以我想选择儿童填充集合中的字段,因为 productos 字段带来了很多文档。
我怎么会有这样的输出......
{
data: "Electronic",
label: "Electronic",
children: [
{
data: "Mobiles",
label: "Mobiles",
children: [
{
data: "Samsung",
label: "Samsung",
children: [],
eventos: true;
tienda: true;
}
],
eventos: true;
tienda: true;
}
],
eventos: true;
tienda: true;
}
解决方案
我找到了解决方案。
children: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'categorias',
autopopulate: { select: '-productos' },
}],
autopopulate 接受选项,然后您可以选择或取消选择要排除或添加的字段。
推荐阅读
- nearprotocol - NEAR 协议网络的发布周期是多少?
- postgresql - Postgres RDS pg_recvlogical 复制角色
- r - 如何制作支持带引号和不带引号的参数的 tidyverse 函数?
- json - 如何使用空键并使用默认值快速读取 json?
- javascript - 带有嵌套函数和循环的 ES6 闭包的奇怪输出,为什么输出与 Var 不同而与 Let 不同?
- c# - 在 Html.BeginForm 中使用局部视图将空模型发送到控制器操作方法
- html - 网页材料设计 - 如何进行自动完成输入
- sql - Oracle SQL - 模型子句中的可变列数
- java - 如何将类类型作为参数传递给函数?
- c - 找不到我的猜谜游戏的解决方案