whathetech Database,Mongodb,node.js MongoDB Remove Document

MongoDB Remove Document

To understand how to insert a document, you can go though http://whatthetech.co/mongoose-insert-document-using-insertmany-using-node-js/
Let us first run the command directly using mongo client.

From the terminal just type

home@home:~/projects/whatthetech/mongoose$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2018-01-24T19:47:40.708+0530 I STORAGE  [initandlisten]
2018-01-24T19:47:40.708+0530 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-01-24T19:47:40.708+0530 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten]
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten]
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten]
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten]
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-01-24T19:47:44.123+0530 I CONTROL  [initandlisten]
>
>
> use example
switched to db example
> show collections
youtubes

Lets now check the number for documents that are in our collection and their IDs

> db.youtubes.find({}, {_id: 1}).pretty()
{ "_id" : ObjectId("59ef5cfb4b821c30d7c63bde") }
{ "_id" : "1001" }
{ "_id" : "1002" }
{ "_id" : "1003" }
{ "_id" : "1004" }
{ "_id" : ObjectId("5a6a97edc8255b21387168a4") }
{ "_id" : ObjectId("5a6a9820c8255b21387168a5") }
{ "_id" : ObjectId("5a6a998bc8255b21387168a6") }
{ "_id" : ObjectId("5a6a999ec8255b21387168a7") }
{ "_id" : ObjectId("5a6a99fec8255b21387168a8") }

Now, lets remove the first document

> db.youtubes.remove({_id: ObjectId("59ef5cfb4b821c30d7c63bde")})
<strong>WriteResult({ "nRemoved" : 1 })</strong>
> db.youtubes.find({}, {_id: 1}).pretty()
{ "_id" : "1001" }
{ "_id" : "1002" }
{ "_id" : "1003" }
{ "_id" : "1004" }
{ "_id" : ObjectId("5a6a97edc8255b21387168a4") }
{ "_id" : ObjectId("5a6a9820c8255b21387168a5") }
{ "_id" : ObjectId("5a6a998bc8255b21387168a6") }
{ "_id" : ObjectId("5a6a999ec8255b21387168a7") }
{ "_id" : ObjectId("5a6a99fec8255b21387168a8") }
>

We have successfully removed one document from our db.
Let us now do the same using Node.JS
For this, we will go back to server.js and create an end point /remove

 //server.js
        app.get('/remove', function (req, res) {
            let id = req.query.ids;
            console.log('ID to be removed = ', id);
            dao.remove(id).then(response=>{
                res.status(200).send('Successfully removed document from DB');
            })
            .catch(err=>{
                console.log(err);
                res.status(400).send('Failed to remove');
            });
            res.status(200).send('Inserted new doc');
        });

Update the dao.js to accept the request and remove the document from DB

 //dao.js
    remove: function(videoID) {
        return new Promise(function(resolve, reject) {
            let query = {_id: videoID};
            Youtube.remove(query, function (err, response) {
                if (err) {
                    return reject(err);
                }
                return resolve();
            });
        });
    }

Now that everything is ready, send a GET request to delete the document.
From the list of Ids above, we will remove _id=1001

http://localhost:3000/remove?ids=1001

Check if the document has actually been removed from DB

> db.youtubes.find({}).count()
8
> db.youtubes.find({}, {_id: 1}).pretty()
{ "_id" : "1002" }
{ "_id" : "1003" }
{ "_id" : "1004" }
{ "_id" : ObjectId("5a6a97edc8255b21387168a4") }
{ "_id" : ObjectId("5a6a9820c8255b21387168a5") }
{ "_id" : ObjectId("5a6a998bc8255b21387168a6") }
{ "_id" : ObjectId("5a6a999ec8255b21387168a7") }
{ "_id" : ObjectId("5a6a99fec8255b21387168a8") }
>

As you can see, we have successfully removed id: 1001 from our DB

Tags: , , ,