猫鼬填充和 return 一个以路径作为列表的文档
mongoose populate and return a document with path as a list
我正在使用猫鼬 5.9.28 和节点 v12.16.1。
我需要编写一个将列表作为参数并在猫鼬模型中填充该列表的函数。 (模型在功能上是常量)
我的架构:
var schema = new mongoose.Schema({
id : {
type : String,
required : true,
unique : true,
},
driverId : {
type : mongoose.Schema.Types.ObjectId,
ref : "drivers"
},
vehicleId : {
type : mongoose.Schema.Types.ObjectId,
ref : "vehicles"
},
customerId : {
type : mongoose.Schema.Types.ObjectId,
ref : "customers",
required : true
},
bookedOn : {
type : String
},
pickUpLocation : {
type : String,
required : true,
},
dropLocation : {
type : String,
required : true
},
paymentId : {
type : mongoose.Schema.Types.ObjectId,
ref : "payments"
},
bookingStatusId :{
type : mongoose.Schema.Types.ObjectId,
ref : "booking_status"
},
goodsType : {
type : String,
required : true
}
});
这里的driverId, vehicleId, customerId, paymentId, bookingStatusId是对其他模型的引用
我有这个函数,其中 refs 是一个列表。
const getBookings = async (refs) => {
const booking = await bookingModel.find().lean().populate({
path : refs,
select : ['-_id']
).exec()
return booking;
}
如果我调用 getBookings(['customerId','driverId'])
,我应该得到包含填充的客户和驱动程序详细信息的文档,不包括 _id。
但我得到的错误是 TypeError: utils.populate: invalid path. Expected string. Got typeof "object"
。
如有任何帮助,我们将不胜感激。提前致谢
Mongoose Model#populate
接受单个 path
。要填充多个字段,您需要使用链接 here in the docs.
您可以 运行 在 refs
数组上循环以链式填充模型。类似于:
const getBookings = async (refs) => {
let query = bookingModel.find().lean();
refs.forEach((ref => query = query.populate({path: ref, select: ['-_id'] });
const booking = await query.exec()
return booking;
}
mongoose populate 方法只接受一个字符串,它是你要引用的字段的名称。所以你不能传递一个包含你想要填充的所有字段名称的列表。您需要在单独的方法调用中传递要填充的字段的每个名称。查看以下代码片段:
const booking = await bookingModel.find()
.lean()
.populate({ path: 'customerId', select: ['_id']})
.populate({ path: 'driverId', select: ['_id']})
.exec();
另请参阅文档:https://mongoosejs.com/docs/populate.html#populating-multiple-paths
我正在使用猫鼬 5.9.28 和节点 v12.16.1。
我需要编写一个将列表作为参数并在猫鼬模型中填充该列表的函数。 (模型在功能上是常量)
我的架构:
var schema = new mongoose.Schema({
id : {
type : String,
required : true,
unique : true,
},
driverId : {
type : mongoose.Schema.Types.ObjectId,
ref : "drivers"
},
vehicleId : {
type : mongoose.Schema.Types.ObjectId,
ref : "vehicles"
},
customerId : {
type : mongoose.Schema.Types.ObjectId,
ref : "customers",
required : true
},
bookedOn : {
type : String
},
pickUpLocation : {
type : String,
required : true,
},
dropLocation : {
type : String,
required : true
},
paymentId : {
type : mongoose.Schema.Types.ObjectId,
ref : "payments"
},
bookingStatusId :{
type : mongoose.Schema.Types.ObjectId,
ref : "booking_status"
},
goodsType : {
type : String,
required : true
}
});
这里的driverId, vehicleId, customerId, paymentId, bookingStatusId是对其他模型的引用
我有这个函数,其中 refs 是一个列表。
const getBookings = async (refs) => {
const booking = await bookingModel.find().lean().populate({
path : refs,
select : ['-_id']
).exec()
return booking;
}
如果我调用 getBookings(['customerId','driverId'])
,我应该得到包含填充的客户和驱动程序详细信息的文档,不包括 _id。
但我得到的错误是 TypeError: utils.populate: invalid path. Expected string. Got typeof "object"
。
如有任何帮助,我们将不胜感激。提前致谢
Mongoose Model#populate
接受单个 path
。要填充多个字段,您需要使用链接 here in the docs.
您可以 运行 在 refs
数组上循环以链式填充模型。类似于:
const getBookings = async (refs) => {
let query = bookingModel.find().lean();
refs.forEach((ref => query = query.populate({path: ref, select: ['-_id'] });
const booking = await query.exec()
return booking;
}
mongoose populate 方法只接受一个字符串,它是你要引用的字段的名称。所以你不能传递一个包含你想要填充的所有字段名称的列表。您需要在单独的方法调用中传递要填充的字段的每个名称。查看以下代码片段:
const booking = await bookingModel.find()
.lean()
.populate({ path: 'customerId', select: ['_id']})
.populate({ path: 'driverId', select: ['_id']})
.exec();
另请参阅文档:https://mongoosejs.com/docs/populate.html#populating-multiple-paths