Created
August 4, 2017 21:25
-
-
Save taesup/eeedd94cedff338401be1d4cc748ce33 to your computer and use it in GitHub Desktop.
Mongo Notes
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
var MongoClient = require('mongodb').MongoClient; | |
// Connection URL | |
var url = 'mongodb://localhost:27017/mongo_demo'; | |
module.exports = function createConnection() { | |
} | |
// Use connect method to connect to the server | |
MongoClient.connect(url, function(err, db) { | |
console.log("Connected successfully to server"); | |
// Get the documents collection | |
var collection = db.collection('mongo_demo_collection'); | |
// // Insert some documents | |
// collection.insertMany([ | |
// {a : 1}, {a : 2}, {a : 3} | |
// ], function(err, result) { | |
// console.log("Inserted 3 documents into the collection"); | |
// console.log(result); | |
// db.close(); | |
// }); | |
// Insert some documents | |
collection.insertOne({a : 4}, function(err, result) { | |
console.log("Inserted 1 document into the collection"); | |
// console.log(result); | |
collection.find({a:1}).limit(1).toArray() | |
.then((result) => { | |
console.log(result); | |
db.close(); | |
}); | |
// collection.findOne({a:1}) | |
// .then((result) => { | |
// console.log(result); | |
// db.close(); | |
// }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment