node.js - Using a value of an document as _id when importing this document to MongoDB using NodeJS -
i'm trying import object mongo. when try use value of object _id fails. error i.e: "[casterror: cast objectid failed value "11563195" @ path "_id"]" , later "[error: document must have _id before saving]"
what doing wrong ?
// read , import 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); })
here used schema:
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);
many time , help.
by default mongoose validating _id field mongoid. if want store other mongoid in _id field need give _id field different type.
var someschema = new schema({ _id: { type: string, required: true } }
Comments
Post a Comment