Populating referenced collections in MongoDB from JSON files using Node.js and Mongoose -


i populating referenced collections in mongodb json files using node.js , mongoose

var userschema = new schema({ username: string, sites : [{type: mongoose.schema.types.objectid, ref: 'site' }],  });  var siteschema = new schema({ user : { type: string, ref: 'user' }, sitetitle: string,  pages : [{type: mongoose.schema.types.objectid, ref: prefix + 'page' }], });  var pageschema = new schema({ site: { type: string, ref: 'site' }, title: string, });  exports.create = function(req, res, next) { var user = new user(req.body); user.save(function(err) { // create user      if (err) {         return next(err);     } else {         var site = new site({user: user.username, sitetitle: user.username});         site.save(function(err) { // create website             if (err) {                 return next(err);             } else {                 user.sites.push(site); // push site'id in sites field in user                 user.save(); // save user after site'id has been push                  (var i=0; i<basepages.length; i++) { // create pages base.json file                     var page = new page(basepages[i]);                     page.save(function(err) { // create page                         if (err) {                             return next(err);                         } else {                             site.pages.push(page);                             site.save();                         }                     });                    res.json(user);                 }             }         });     }; }); }; 

pages created json file , pages field in site document populated have 1 id. mean json file has 2 pages

pages: "_id123456", "_id123456" from

site.pages.push(page); site.save(); 

here solution found myself :) add page argument in page.save function;

exports.create = function(req, res, next) { var user = new user(req.body); user.save(function(err) { // create user      if (err) {         return next(err);     } else {         var site = new site({user: user.username, sitetitle: user.username});         site.save(function(err) { // create website             if (err) {                 return next(err);             } else {                 user.sites.push(site); // push site'id in sites field in user                 user.save(); // save user after site'id has been push                 (var i=0; i<basepages.length; i++) { // create pages base.json file                     var page = new page(basepages[i]);                     page.save(function(err, **page**) { // create page                         if (err) {                             return next(err);                         } else {                             site.pages.push(**page**);                             site.save();                         };                     });                 };                 res.json(user);             };         });     }; }); }; 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -