我是 Node.js 和 mongodb 的新手。
在我的 Node.js 应用程序中,我使用 mongodb,对于 mongodb 操作,我使用 mongoose。
在 package.json
文件中,我有以下依赖项:
"mongoose": "4.11.9"
虽然我的应用程序可以运行(我可以做我想做的一切),但在服务器日志中我总是看到以下错误消息:
To create a new ObjectId please try
Mongoose.Types.ObjectId
instead of usingMongoose.Schema.ObjectId
.
如何解决这个问题?
最佳答案
它已在 mongoose 4.11.10
及更高版本中修复,因此请尝试升级您的版本以消除警告。
这是 #5571 的无意副作用因为 employees.certifications
恰好是 24 个字符长。
var mongoose = require('mongoose');
var zoneSchema = new mongoose.Schema({
employees: {
certifications: {type: mongoose.Schema.Types.ObjectId, ref: 'Certifications'}
}
});
var certificationsSchema = new mongoose.Schema({
type: String,
list: [{type: mongoose.Schema.Types.ObjectId, ref: 'Certification'}]
});
var certificationSchema = new mongoose.Schema({
name: String
});
var Zone = mongoose.model('Zone', zoneSchema);
var Certifications = mongoose.model('Certifications', certificationsSchema);
var Certification = mongoose.model('Certification', certificationSchema);
var newZone = new Zone();
如需进一步阅读,请查看 issue#5587 & issue#5571
关于node.js - MongoDb - 要创建新的 ObjectId,请尝试 `Mongoose.Types.ObjectId` 而不是使用 `Mongoose.Schema.ObjectId`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49841247/