Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mongoose填充并返回路径为列表的文档_Javascript_Node.js_Mongodb_Mongoose_Mongoose Populate - Fatal编程技术网

Javascript mongoose填充并返回路径为列表的文档

Javascript mongoose填充并返回路径为列表的文档,javascript,node.js,mongodb,mongoose,mongoose-populate,Javascript,Node.js,Mongodb,Mongoose,Mongoose Populate,我正在使用mongoose 5.9.28和节点v12.16.1 我需要编写一个函数,将一个列表作为参数,并在mongoose模型中填充该列表。(模型在功能上是恒定的) 我的模式: var schema = new mongoose.Schema({ id : { type : String, required : true, unique : true, }, driverId : { type :

我正在使用mongoose 5.9.28和节点v12.16.1

我需要编写一个函数,将一个列表作为参数,并在mongoose模型中填充该列表。(模型在功能上是恒定的)

我的模式:

    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。应为字符串。获取“对象”的类型

任何帮助都将不胜感激。提前感谢

猫鼬
模型#填充
接受单个
路径
。要填充多个字段,需要使用前面提到的链接

可以在
refs
数组上运行循环,以链填充模型。比如:

const getBookings=async(参考)=>{
let query=bookingModel.find().lean();
forEach((ref=>query=query.populate({path:ref,select:['-\u id']});
const booking=wait query.exec()
回程订票;
}
Mongoose
Model#populate
接受单个
路径
。要填充多个字段,需要使用前面提到的链接

您可以在
refs
数组上运行循环以链填充模型。类似于:

const getBookings=async(参考)=>{
let query=bookingModel.find().lean();
forEach((ref=>query=query.populate({path:ref,select:['-\u id']});
const booking=wait query.exec()
回程订票;
}

mongoose populate方法只接受字符串,该字符串是您要引用的字段的名称。因此,您无法传递包含所有要填充的字段名称的列表。您需要在单独的方法调用中传递要填充的字段的每个名称。请查看以下代码段:

const booking = await bookingModel.find()
                           .lean()
                           .populate({ path: 'customerId', select: ['_id']})
                           .populate({ path: 'driverId', select: ['_id']})
                           .exec();


另请参见文档:

mongoose populate方法只接受字符串,该字符串是您要引用的字段的名称。因此,您无法传递包含所有要填充的字段名称的列表。您需要在单独的方法调用中传递要填充的字段的每个名称。请查看以下代码片段:

const booking = await bookingModel.find()
                           .lean()
                           .populate({ path: 'customerId', select: ['_id']})
                           .populate({ path: 'driverId', select: ['_id']})
                           .exec();

另请参见文档中的: