node.js - inserting document of collections to another collections in mongodb -
i have 2 different collection of documents in mongodb database. in recruits collection have plenty of documents. , want add document recruit collections employee collections here part of code
function register(){ $http({ url:'/employee', method:'post', data:data // data findbyid query. }).then(function(result){ vm.cancel(); vm.toast(); },function(err){ console.log(err); })
here data findbyid recruits collections. same data want store in employees collection.i tried so:
var emp = require('../models/employees.js'); router.post('/',function(req,res){ console.log(req.body); var register = new emp(req.body); //console.log(register); register.save(function(err, respond) { if (err) { throw err; } else { res.json(respond); } }) });
i think fine code console shows me
mongoerror: e11000 duplicate key error index: humanresource.employees.$_id_ dup key: { : objectid('57395146b4c73790056a3770') }
any 1 suggest me how insert such document in collections.
you error saying document creating has _id exists in employees collection. problem receiving recruit data , directly passing employees collection along _id passed recruit. should try copying data excluding _id field. mongo automatically generate _id employees collection. this,
var emp = require('../models/employees.js'); router.post('/',function(req,res){ console.log(req.body); delete req.body._id; var register = new emp(req.body); register.save(function(err, respond) { if (err) { throw err; } else { res.json(respond); } });
Comments
Post a Comment