Created
May 1, 2020 15:58
-
-
Save camilomontoyau/259027646cd699f2c97e3a8689388756 to your computer and use it in GitHub Desktop.
ejemplo conexión mongo con mongoose y nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
(async()=>{ | |
await mongoose.connect('mongodb://nodeco-test-user:nodecotest1234@ds163822.mlab.com:63822/nodeco-test-01-mayo', { | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}); | |
})() | |
const Schema = mongoose.Schema; | |
const ObjectId = Schema.ObjectId; | |
const BlogPost = new Schema({ | |
title: String, | |
body: String, | |
date: Date | |
}); | |
const BlogPostModel = mongoose.model('blospost', BlogPost); | |
const blogPost = new BlogPostModel({ | |
title: 'titulo2', | |
body: 'esto es un blog post 222222', | |
date: new Date() | |
}); | |
(async ()=>{ | |
await blogPost.save(); // espera a guardar el documento | |
await mongoose.disconnect(); // cierra la conexion | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment