我怎样才能 return 一个 mongo 填充调用的子集?
how can I return a subset of a mongo populate call?
我正在使用 mongo db 'ref' 功能将 table 中的对象填充到我正在 returning
的文档中
这里是存储对象ID ref
的table
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const StaticAssignmentSchema = new Schema({
affiliation: { type: String },
REF_person: [{ type: Schema.Types.ObjectId, ref: 'users' }],
});
module.exports = StaticAssignmentNew = mongoose.model('staticassignments', StaticAssignmentSchema);
这是我的用户 table 架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: { type: String },
phone: { type: String },
password: { type: String },
affiliation: { type: String },
timezone: { type: String },
date: { type: Date, default: Date.now },
});
module.exports = User = mongoose.model('users', UserSchema);
这是我从 mongo
获取数据的方式
var query = { affiliation: affiliation };
var ret = await StaticAssignment.findOne(query).populate('REF_person');
其中 return 如下:
{
"_id": "61a8376154377e5704ae79cf",
"affiliation": "800_800",
"REF_person": [{
"_id": "5e441bd5332359211c0403fb",
"name": "Frank",
"phone": "8008008001",
"password": "a$T9wYLE3v/n8S4GB.oyYOmCsGUdxXQiOjpiDaG0JGbKy",
"affiliation": "800_800",
"timezone": "America/Los_Angeles",
"date": "2020-02-12T15:37:57.167Z"
}]
}
但是,我想 return 填充调用的一个子集:
{
"_id": "61a8376154377e5704ae79cf",
"affiliation": "800_800",
"REF_person": [{
"_id": "5e441bd5332359211c0403fb",
"name": "Frank",
"phone": "8008008001",
}]
}
我可以对 return 子集做什么?
谢谢楼主!
答案:
var query = { affiliation: affiliation };
var ret = await StaticAssignment.findOne(query)
.populate({path: 'REF_person', select: '_id name phone'});
尝试var ret = await StaticAssignment.findOne(query).populate({path: 'REF_person', select: 'name phone'});
查看此页面:https://www.codegrepper.com/code-examples/whatever/mongoose+populate+select+fields
我正在使用 mongo db 'ref' 功能将 table 中的对象填充到我正在 returning
的文档中这里是存储对象ID ref
的tableconst mongoose = require('mongoose');
const Schema = mongoose.Schema;
const StaticAssignmentSchema = new Schema({
affiliation: { type: String },
REF_person: [{ type: Schema.Types.ObjectId, ref: 'users' }],
});
module.exports = StaticAssignmentNew = mongoose.model('staticassignments', StaticAssignmentSchema);
这是我的用户 table 架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: { type: String },
phone: { type: String },
password: { type: String },
affiliation: { type: String },
timezone: { type: String },
date: { type: Date, default: Date.now },
});
module.exports = User = mongoose.model('users', UserSchema);
这是我从 mongo
获取数据的方式 var query = { affiliation: affiliation };
var ret = await StaticAssignment.findOne(query).populate('REF_person');
其中 return 如下:
{
"_id": "61a8376154377e5704ae79cf",
"affiliation": "800_800",
"REF_person": [{
"_id": "5e441bd5332359211c0403fb",
"name": "Frank",
"phone": "8008008001",
"password": "a$T9wYLE3v/n8S4GB.oyYOmCsGUdxXQiOjpiDaG0JGbKy",
"affiliation": "800_800",
"timezone": "America/Los_Angeles",
"date": "2020-02-12T15:37:57.167Z"
}]
}
但是,我想 return 填充调用的一个子集:
{
"_id": "61a8376154377e5704ae79cf",
"affiliation": "800_800",
"REF_person": [{
"_id": "5e441bd5332359211c0403fb",
"name": "Frank",
"phone": "8008008001",
}]
}
我可以对 return 子集做什么?
谢谢楼主!
答案:
var query = { affiliation: affiliation };
var ret = await StaticAssignment.findOne(query)
.populate({path: 'REF_person', select: '_id name phone'});
尝试var ret = await StaticAssignment.findOne(query).populate({path: 'REF_person', select: 'name phone'});
查看此页面:https://www.codegrepper.com/code-examples/whatever/mongoose+populate+select+fields