Skip to main content

MongoDB


MongoDB.

MongoDB is a document database. It is a very flexible database system because it stores data in JSON-like documents and It allows to change the data structure over time.
It's  fully packed with a function system that makes development easy.
MongoDB supports to over 10 languages. It contains drivers to most of the popular languages and it's growing fast with the help of the community.

Connecting to the MongoDB.

You can connect to a locally hosted MongoDB database with JavaScript by using below code.

Database name is = 'techciv'
Collection name is = 'posts'

var url = 'mongodb://localhost:27017/techciv'; co(function*() { const db = yield MongoClient.connect(url); console.log("Connected"); yield insertDocuments(db, null); yield findDocuments(db, null); yield indexCollection(db, null); yield aggregateDocuments(db, null); db.close(); }).catch(err => console.log(err));

Insert documents to MongoDB

var insertDocuments = function(db, callback) { return co(function*() { const results = yield db.collection('posts').insertMany([ { "title": "Test title 1", "status": 0, "postData": [ "data 1", "data 2", "data 3", "data 4" ] }, { "title": "Test title 2", "status": 1, "postData": [ "data 1", "data 2", "data 3", "data 4" ] } ]); console.log(results) return results; });
};

You can find all functions of MongoDB from the following link.

Documentations for MongoDB : https://docs.mongodb.com/




Comments

Post a Comment