【发布时间】:2016-10-14 19:32:34
【问题描述】:
我想将通过 REST 接口通过 mongoose 获得的 ObjectId 数组保存到 mongodb。我经常遇到将objectIds从REST接口保存到数据库时出现转换错误的问题。服务器端代码是用 typescript 编写的。
架构是:
var WaSchema = new mongoose.Schema({
ownerId: { type: 'String', required: true },
options: { type: 'String', required: false },
launch: [{ type : 'ObjectId', required: true }],
});
从 REST 接口中,我得到一个“lanch”字符串数组:launch: Array<string>
这是我目前的保存方式:
WaModel.findOneAndUpdate(query, {
ownerId: userId,
options: wa.options,
launch: wa.launch
},
{ upsert: true },
(err, doc) => {
if (err) throw err
else return 'successfully saved/updated';
})
REST 接口中的 ObjectId 必须如何看起来才能正确转换?它只是像'575e52790c0fc76a11e381d0'这样的刺痛还是需要像ObjectId(“575e52790c0fc76a11e381d0”)这样的前缀?
数组最后会是什么样子?这取决于#1的答案
我看到了填充功能,这对这里有帮助吗?
【问题讨论】:
标签: node.js mongodb mongoose typescript mongoose-populate