Skip to content

Instantly share code, notes, and snippets.

@camilomontoyau
Created May 1, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilomontoyau/259027646cd699f2c97e3a8689388756 to your computer and use it in GitHub Desktop.
Save camilomontoyau/259027646cd699f2c97e3a8689388756 to your computer and use it in GitHub Desktop.
ejemplo conexión mongo con mongoose y nodejs
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