Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Last active April 4, 2020 07:17
Show Gist options
  • Save manojnaidu619/cbb2dc0f1e8a924c7f45a7a014808ae7 to your computer and use it in GitHub Desktop.
Save manojnaidu619/cbb2dc0f1e8a924c7f45a7a014808ae7 to your computer and use it in GitHub Desktop.
Mongoose boilerplate
const mongoose = require("mongoose")
mongoose.connect("mongodb://127.0.0.1:27017/task-manager-api", { // establishing DB connection
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
const User = mongoose.model('User', {
name: {
type: String // Validation
},
age: {
type: Number // Validation
}
})
const record = new User({ // New model instance
name: "Manoj",
age: 20
}).save().then((res) => console.log(res)).catch((err)=> console.log(err)) // save() returns a promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment