
So in my course about nodejs I´m working with MongoDB. So I decided to documented my new knowledge here. In this post, I´ll provide an overview of MongoDB and some of its key features.
What is MongoDB ?
It is a document-oriented database, not a relational one. Non relational means it does not store data in tables but in the form of JSON document. The primary reason for moving away from the relational model is to make scaling out easier. Here in MongoDB row is replaced with document and table is replaced with collection. It can be thought of as a group of documents.
Document is the basic unit of data for MongoDB, roughly equivalent to a row. It is a data structure composed of field and value pairs.
MongoDB documents are similar to JSON objects. The values maybe documents, arrays, and arrays of documents.
Here is what we cover in this Post
- DELETE
- FIND
- UPDATE
1. DELETE
We are going to cover three delete Methods whichs MongoDB provides.
- deleteMany – Deletes every document where text is the queried one
- deleteOne – Deletes every document where text is the queried one
- findOneAndDelete – Finds the first document where completed is false and return/delete it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// ------------------------------ DELETE -------------------------------------- const { MongoClient, ObjectID } = require('mongodb'); MongoClient.connect('mongodb://localhost:27017/TodoApp', ( err, client ) => { if ( err ) { console.log( 'Unable to connect to mongodb server' ); } console.log( 'Connect to MongoDB server' ); const db = client.db( 'TodoApp' ); // deleteMany -> Deletes every document where text is 'Todo from postman' db.collection('Todos').deleteMany( { text: 'Todo from postman' } ).then( ( result ) => { console.log( result ); // -> returns result Object - link }); // deleteOne -> Deletes the first docuemnt where tesxt is 'Todo from postman1' db.collection('Todos').deleteOne( { text: 'Todo from postman1' } ).then( ( result ) => { console.log( result ); // -> returns result Object }) // findOneAndDelete -> Finds the first document where completed is false and return/delete it db.collection('Todos').findOneAndDelete( { completed: false } ).then( ( result ) => { console.log( result ); // -> returns { lastErrorObject: { n: 0 }, value: null, ok: 1 } }); client.close(); }); |
2. FIND
We are going to cover one delete Methods whichs MongoDB provides:
- find – Gets all todos and can be filtered
- findOne – takes first argument as a query string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// ------------------------------ FIND -------------------------------------- const {MongoClient, ObjectID} = require('mongodb'); // Connection to MongoDB MongoClient.connect('mongodb://localhost:27017/TodoApp', ( err, client ) => { if ( err ) { console.log( 'Unable to connect to mongodb server' ); } console.log( 'Connect to MongoDB server' ); const db = client.db('TodoApp'); // find -> Gets all Todos and count it db.collection('Todos').find().count().then( ( count ) => { console.log('Todos count:', count); }, ( err ) => { console.log('Unable to fetch data', err) }); // find -> Gets all Users where the name is Moritz Vogt db.collection('Users').findOne( { name: 'Moritz Vogt' } ).then( ( user ) => { console.log( user ); }, ( err ) => { console.log('Unable to fetch data', err) }); client.close(); }); |
3. UPDATE
We are going to cover one delete Methods whichs MongoDB provides :
- findOneAndUpdate – Its the same as findOneAndDelete but this time we update a Document so me mus provide Information which attributes should get updated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ------------------------------ UPDATE -------------------------------------- const {MongoClient, ObjectID} = require('mongodb'); // Connection to MongoDB MongoClient.connect('mongodb://localhost:27017/TodoApp', ( err, client ) => { if ( err ) { console.log( 'Unable to connect to mongodb server' ); } console.log( 'Connect to MongoDB server' ); const db = client.db('TodoApp'); // findOneAndUpdate - finds the document with _id and sets name to Jens Müller // and increments age db.collection('Users').findOneAndUpdate({ _id: new ObjectID("5b61e611857666516a7bd573") }, { $set: { name: 'Jens Müller' }, $inc: { age: 1 } }, { returnOriginal: false // returns the not changed document if its set to true }).then( ( result ) => { console.log( result ) }); client.close(); }); |
I got to know these methods in my course, I hope you have no an understanding overview about MongoDB and its Methods.
Greetings
Moritz