Correct way to seed MongoDB with references via mongoose

Related searches

I have three schemas, one which references two others:

userSchema

{ name: String }

postSchema

{ content: String }

commentSchema

{ 
  content: String,
  user: { ObjectID, ref: 'User' },
  post: { ObjectID, ref: 'Post' }
}

How can I seed this database in a sane, scalable way? Even using bluebird promises it quickly becomes a nightmare to write.

My attempt so far involves multiple nested promises and is very hard to maintain:

User
.create([{ name: 'alice' }])
.then(() => {
    return Post.create([{ content: 'foo' }])
})
.then(() => {
  User.find().then(users => {
    Post.find().then(posts => {
      // `users` isn't even *available* here!
      Comment.create({ content: 'bar', user: users[0], post: posts[0] })
    })
  })
})

This is clearly not the correct way of doing this. What am I missing?

Not sure about bluebird, but the nodejs Promise.all should do the job:

Promise.all([
    User.create([{ name: 'alice' }]),
    Post.create([{ content: 'foo' }])
]).then(([users, posts]) => {
    const comments = [
        { content: 'bar', user: users[0], post: posts[0] }
    ];    
    return Comment.create(comments);
})

Seeding mongoose data models with references. � GitHub, Model.seed = function(entities) { original source http://frederiknakstad.com/ 2013/04/12/seeding-mongodb-through- create model for users via mongoose. Seeding mongoDB data with references. Push items into mongo array via mongoose. Correct way to provide a direct network link between 2 devices as well as

If you want to seed database with automatically references, use Seedgoose. This is the easiest seeder for you to use. You don't need to write any program files, but only data files. And Seedgoose handles smart references for you. And by the way, I'm the author and maintainer of this package.

Seeding mongodb through your mongoose models, How did you reference an exercise in workoutTemplates.json? Frederik Nakstad • 5 years ago. I do this by generating an _id field for all the� Wondering what is the proper way to seed the database, and use the db schemas already defined to ensure the data seeded is valid and properly maintained. Seeding at the module level (just before the definition of the Module) feels hacky and ends in threadpool being destroyed, and therefore all following mongo operations fail

Try this it will work fine:

Note: Node Promise.all will make sure that the both query is executed properly and then return the result in Array:[Users, Posts], If you get any error during execution of any query, it will be handle by catch block of the Promise.all.

let queryArray = [];
queryArray.push(User.create([{ name: 'alice' }]));
queryArray.push(Post.create([{ content: 'foo' }]));

Promise.all(queryArray).then(([Users, Posts]) => {
const comments = [
    { content: 'bar', user: Users[0], post: posts[0] }
];   
return Comment.create(comments);
}).catch(Error => {
   console.log("Error: ", Error);
})

Manual, mongoose-data-seed. Seed mongodb with data using mongoose models. cli example using md-seed run. Install. npm install --save mongoose-data-seed� I'm using mongoose to seed a mongodb database based on my mongoose models. I'm particular interested in improving the seed code. I have two models, an article and a category. The relationship between the models are as follows: A category has many articles An article has a parent category. The model schemas I'm using are listed below: The

Seeding MongoDB database the right way | by Paweł Kosiec, Mongo Seeding is MIT licensed, so you can use it freely in any commercial and non-commercial project. So, how it works? Describe import data� md-seed-config.js. md-seed expecting to get 3 values from md-seed-config.js. seedersList - A key/value list of all your seeders, md-seed will run your seeders as they ordered in the list. connect - Connect to mongodb implementation (should return promise). dropdb - Drop/Clear the database implementation (should return promise). Example

Build database relationships with Node.js and MongoDB, Let's dive deeper into MongoDB and understand how relationships work between For the author model, we define a model schema that can reference book models defined, it's time to return and show the data via a JSON API. To fix that, we'll need to set up code to seed the database with records. seedersList - A key/value list of all your seeders, md-seed will run your seeders as they ordered in the list. connect - Connect to mongodb implementation (should return promise). dropdb - Drop/Clear the database implementation (should return promise).

The seed function has two options. data: The JSON objects that will be used to create the mongo documents. options: [optional] Extra options that alter the behaviour. The default behaviour is drop the entire database before seeding it again. Callbacks. Although, promises are the preferred way of using the library.

Comments
  • I've seen this plugin. Is this the answer to spare my sanity?
  • This looks like what I'm looking for. Can you then chain an additional .then which will wait until the Comments are created?
  • This looks good. You might want to mention that you're the author of this tool. (That's not a problem, but it's worth mentioning!)