mongodb - Mongoose Import Json Export with IDs -


i want import unit tests fixed limited subset of actual database. exported database mongo shell mydata.json. want read array of json files db, keeping ids.

1st: fail on reading json export, how fix this?

if !db?   mongoose.connect(configdb.url,{auth:{authdb:configdb.authdb}}, (err)->     if (err)       console.log(err)   )   db = mongoose.connection   db.on('error', console.error.bind(console, 'connection error:'))   db.once('open', () ->     console.log "database established"     #delete data , seed new data     somemodel = require(applicationdir + 'node/models/some.model.js')     somemodel.remove({}, (err) ->       console.log('collection somes removed seeding new one')       fs.readfile(__dirname + '/../../mongo/seed-for-test/somes.json','utf-8', (err,filedata) ->         console.log typeof filedata         filedata = json.parse(filedata)         console.log filedata.length         # new somemodel(filedata).save((err) ->         #   if err?         #     return console.log err         #   console.log('somes saved')         # )       )     )   ) 

error

string undefined:2 { "_id" : { "$oid" : "551d82e30287751fa2f2dfb2" }, "prooven" : true, "title" : ^ syntaxerror: unexpected token {   @ object.parse (native)   @ /users/masterg/desktop/projekte/lek/specs/backend/mongo.service.spec.js:37:27   @ fs.js:336:14   @ /users/masterg/desktop/projekte/lek/node_modules/wiredep/node_modules/bower-config/node_modules/graceful-fs/graceful-fs.js:104:5   @ fsreqwrap.oncomplete (fs.js:99:15) 

2nd if uncomment lower part work or there else need do.

edit

the export not give valid array of json objects. --jsonarray flag has used when exporting.

this works exporting flag --jsonarray looks wrong me. .json file not formated nicely before. , need add logic check if last entry saved.

objectid = require('mongoose').types.objectid somemodel = require(applicationdir + 'node/models/some.model.js')  if !db?   mongoose.connect(configdb.url,{auth:{authdb:configdb.authdb}}, (err)->     if (err)       console.log(err)   )   db = mongoose.connection   db.on('error', console.error.bind(console, 'connection error:'))   db.once('open', () ->     console.log "database established"     #delete data , seed new data     somemodel.remove({}, (err) ->       console.log('collection somes removed seeding new one')       fs.readfile(__dirname + '/../../mongo/seed-for-test/somes.json','utf-8', (err,filedata) ->         filedata = json.parse(filedata)         singlefiledata in filedata           singlefiledata._id = new objectid(singlefiledata._id.$oid)           new somemodel(singlefiledata).save((err) ->             if err?               return console.log err             console.log('somes saved')           )       )     )   ) 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -