Skip to content

Instantly share code, notes, and snippets.

@robertklep
Created November 5, 2016 13:48
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 robertklep/1a227242c78d7415407dfdca3617ab5b to your computer and use it in GitHub Desktop.
Save robertklep/1a227242c78d7415407dfdca3617ab5b to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
let Schema = mongoose.Schema({ name : String });
let Model = mongoose.model('MyModel', Schema);
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/foobar');
Model.create({ name : 'Jack' }).then(d => {
console.log('Created new document, _id =', d._id);
// Convert to string and query.
return Model.find({ _id : d._id.toString() });
}).then(d => {
if (d) {
console.log('Found document', d);
} else {
console.log('Unable to find document :(');
}
mongoose.disconnect();
}).catch(e => console.log('Error', e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment