这是我当前的代码:
假设我有以下猫鼬模型:
// This is the car model.
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var Schema = mongoose.Schema;
// Setup a mongoose model, and pass it to module.exports.
module.exports = mongoose.model('Car', new Schema({
manufacturer: String,
model: String,
owners: [{type: ObjectId, ref: 'Owner'}]
}));
我想在我的REST api中使用它,如下所示-在这段代码中,我实质上是在尝试创建car对象的实例,然后将当前的所有者ID传入,成为该数组的第一项。 “所有者列表”
app.post('/cars/create', function(req,res){
if( req.body.manufacturer && req.body.model && req.body.ownerId)
{
newCar = new Car(
manufacturer: req.body.manufacturer,
model: req.body.model,
owners: // NOT SURE WHAT TO DO HERE
});
}
});
owners : new Array(ObjectId.createFromHexString(req.body.ownerId))
注意不要稍后重写它,只需推动即可。从那里开始,一切照旧...随时进行填充,操作和保存。
可以从IIRC的Schema.types.ObjectId获得ObjectId。如果您使用的是Mongo的较旧版本,则也可以使用fromHexString()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句