Skip to content

Instantly share code, notes, and snippets.

@dapangmao
Created February 8, 2016 14:26
Show Gist options
  • Save dapangmao/8703aee62efd5cd9f6fc to your computer and use it in GitHub Desktop.
Save dapangmao/8703aee62efd5cd9f6fc to your computer and use it in GitHub Desktop.
chapter 1
var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/example';
mongodb.MongoClient.connect(uri, function(error, db) {
if (error) {
console.log(error);
process.exit(1);
}
db.collection('sample').insert({ x: 1 }, function(error, result) {
if (error) {
console.log(error);
process.exit(1);
}
db.collection('sample').find().toArray(function(error, docs) {
if (error) {
console.log(error);
process.exit(1);
}
console.log('Found docs:');
docs.forEach(function(doc) {
console.log(JSON.stringify(doc));
});
process.exit(0);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment