AmitDiwan has Published 10744 Articles

Invoke convertToCapped and convert an existing collection to capped in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:30:31

213 Views

To convert an existing collection to capped, use convertToCapped. Let us create a collection with documents −> db.demo260.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47b1181627c0c63e7dba9b") } > db.demo260.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47b11c1627c0c63e7dba9c") } > db.demo260.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ... Read More

Get MongoDB documents that contains specific attributes in array

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:29:26

526 Views

For this, you can use $and along with dot(.) notation. Let us first create a collection with documents −>db.demo2.insertOne({"StudentInformation":[{"StudentName":"John", "StudentAge":21}, {"StudentName":"Mike", "StudentAge":22}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08b56e25ddae1f53b62219") } >db.demo2.insertOne({"StudentInformation":[{"StudentName":"Carol", "StudentAge":19}, {"StudentName":"Bob", "StudentAge":18}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08b58625ddae1f53b6221a") }Following is the query to ... Read More

How to run MongoDB shell using mongos command?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:26:14

785 Views

In order to launch the MongoDB shell, you need to use mongo command. Following is the syntax −>mongoFirst reach the MongoDB bin directory from command prompt as in the below screenshot −Here is the command to launch the mongo shell as in the below screenshot −This will produce the following ... Read More

How to update % printed to Console from MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:24:16

123 Views

To update and print to console from MongoDB script, create a variable and then use the print() method.Let us first create a variable −> var amount=10.58945;Here is the query to update % printed to console −> var amount=10.58945; > print(amount.toFixed(2)+" %");This will produce the following output −10.59 %

MongoDB inverse of query to return all items except specific documents?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:22:09

568 Views

To get documents except some specific documents, use $nor along with $and. Let us first create a collection with documents −> db.demo1.insertOne({"StudentName":"Chris", "StudentMarks":38}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08a4f025ddae1f53b62216") } > db.demo1.insertOne({"StudentName":"David", "StudentMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e08a4f725ddae1f53b62217") } > db.demo1.insertOne({"StudentName":"Mike", "StudentMarks":96}); { ... Read More

How to specify the order in which the query returns matching documents in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:20:52

108 Views

To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is db.collectionName.find().Let us create a collection with documents −> db.demo259.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ae1f1627c0c63e7dba98") } > db.demo259.insertOne({"Subject":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47ae231627c0c63e7dba99") } > ... Read More

How to calculate a sum of specific documents using MongoDB aggregation?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:20:32

581 Views

To sum, use $sum and to get sum of specific documents, you need to group them using $group in MongoDB.Let us first create a collection with documents −>db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol", "Bob"], "UsersDetails":[{"FirstUser":"Carol", "TotalLikes":20}, {"FirstUser":"Bob", "TotalLikes":45}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e084e9125ddae1f53b6220c") } >db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol", "Bob"], "UsersDetails":[{"FirstUser":"Carol", "TotalLikes":60}, {"FirstUser":"Bob", "TotalLikes":50}]}); { ... Read More

How to pull value from array of ObjectIDs in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:19:39

696 Views

To pull value from array of ObjectIDs, use $pull in MongoDB. Let us create a collection with documents −> db.demo258.insertOne({"arrayOfObjectsId":[ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91")]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a8211627c0c63e7dba97") }Display all documents from a collection with the help of find() method −> db.demo258.find();This will produce the following output ... Read More

Set unique index in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:17:56

227 Views

To set unique index in MongoDB, use unique:true. Let us create a collection with documents −> db.demo257.ensureIndex({Name:1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo257.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a5e51627c0c63e7dba91") } > db.demo257.insertOne({Name:"Bob"}); ... Read More

Set MongoDB compound index with a fixed value field

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:17:39

192 Views

For this, we will use the concept of createIndex() and create index −> db.compoundIndexDemo.createIndex({"StudentName":1, "StudentAge":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us first create a collection with documents −> db.compoundIndexDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" ... Read More

Advertisements