在填充时连接一个string

这里是我的代码来填充专辑和他们的图像:

server.get(url_prefix + '/image', function(req, res, next) { var album_id = req.query.album_id Album.findOne( {_id : album_id} ) .populate('images') .exec(function (err, album) { if (album) { return res.send({ status: { error: 0, message: "Successful" }, data: { album: album } }) } else return notify_error(res, "No Results", 1, 404) }) }) 

相册架构:

 var AlbumSchema = new Schema( { userId:{ type: String, trim: true }, albumName:{ type: String, trim: true }, albumDescription:{ type: String, trim: true }, imageCount:{ type: Number, default: 0 }, timestamp:{ type: String, trim: true }, albumThumbnail:{ type: Object }, images : [{ type: Schema.Types.ObjectId, ref: 'Image' } ] }) 

图像模式:

 var ImageSchema = new Schema( { _album : { type: String, ref: 'Album' }, imageName:{ type: String, trim: true }, imageDescription:{ type: String, trim: true }, timestamp:{ type: String, trim: true }, imageURL:{ type: String, trim: true }, imageURLSmall:{ type: String, trim: true }, imageThumbnailURL:{ type: String, trim: true }, likeCount:{ type: Number, default: 0 }, commentCount:{ type: Number, default: 0 }, userLike:{ type: Boolean, default: false } }) 

我能够填充相册和他们的图像,在图像模式imageURL只包含图像名称( image.jpg ),所以当检索我想要它像是http://localhost/assets/profile/image.jpg我必须在填充数据或发送数据之前做这件事,因为这些API被移动设备使用,所以他们需要完整的path来显示图像。

那么如何在填充数据时将string添加到mongodb中的字段?

提前致谢

尝试这个:

 ImageSchema .post('init', function(obj) { var url = 'http://localhost/assets/profile/' obj.imageURL = url + obj.imageURL }) 

每次从数据库中获取对象时,都会执行该函数。