【发布时间】:2015-11-24 18:22:08
【问题描述】:
我正在尝试将一个对象导入 Mongo。但是当我尝试将 Object 的值用作 _id 时,它会失败。错误,即:“[CastError: Cast to ObjectID failed for value "11563195" at path "_id"]" 和后来的 "[Error: document must have an _id before Saving]"
我做错了什么?
// Read and import the CSV file.
csv.fromPath(filePath, {
objectMode: true,
headers: keys
})
.on('data', function (data) {
setTimeout(function(){
var Obj = new models[fileName](data);
Obj._id = Obj.SOME_ID;
Obj.save(function (err, importedObj) {
if (err) {
console.log(err);
} else {
console.log('Import: OK');
}
});
}, 500);
})
这是使用的架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SomeSchema = new Schema(
{
SOME_ID: String,
FIELD01: String,
FIELD02: String,
FIELD03: String,
FIELD04: String,
FIELD05: String,
FIELD06: String,
FIELD07: String,
FIELD08: String,
FIELD09: String,
FIELD10: String,
FIELD11: String,
FIELD12: String,
FIELD13: String
},
{
collection: 'SomeCollection'
});
module.exports = mongoose.model('SomeCollection', SomeSchema);
非常感谢您的时间和帮助。
【问题讨论】:
-
"11563195"不是 ObjectID 的有效值 -
Obj的架构是什么?您要在此处保存的数据是什么? -
@SergioTulentsev 这是我创建的用于从 CSV 导入数据的模式,所有字符串。我需要使用一个字符串,即“11563195”作为_id。不可能吗?
-
应该是可以的。一定是你做错了什么。
-
需要查看您的猫鼬模式。