Found 1349 Articles for MongoDB

How to find minimum value in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

1K+ Views

To find the minimum value in MongoDB, you can use sort() along with limit(1). The syntax is as follows −db.yourCollectionName.find().sort({yourFieldName: 1}).limit(1);To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findMinValueDemo.insertOne({"StudentMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f80ea2f684a30fbdfd59f") } > db.findMinValueDemo.insertOne({"StudentMarks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f80f02f684a30fbdfd5a0") } > db.findMinValueDemo.insertOne({"StudentMarks":79}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f80f32f684a30fbdfd5a1") } > db.findMinValueDemo.insertOne({"StudentMarks":59}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f80f82f684a30fbdfd5a2") } > db.findMinValueDemo.insertOne({"StudentMarks":91}); {    "acknowledged" : true,   ... Read More

How do I display the indexes of a collection in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

270 Views

In order to display the indexes of a collection, you can use getIndexes(). The syntax is as follows −db.yourCollectionName.getIndexes();To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.indexDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f7c4f2f684a30fbdfd599") } > db.indexDemo.insertOne({"StudentName":"Mike", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f7c552f684a30fbdfd59a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.indexDemo.insertOne({"StudentName":"Carol", "StudentAge":20});The following is the output −{    "acknowledged" : true,    "insertedId" : ObjectId("5c8f7c5e2f684a30fbdfd59b") ... Read More

How to list all users in the Mongo shell?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

12K+ Views

In order to list all users in the Mongo shell, use the getUsers() method or show command.Case 1 − Using getUsers()The syntax is as follows −db.getUsers();Case 2 − Using show commandThe syntax is as follows −show users;Let us implement both the syntaxes in order to list all users in the Mongo shell.Case 1 − The first query is as follows −> db.getUsers();The following is the output −[    {       "_id" : "test.John",       "user" : "John",       "db" : "test",       "roles" : [          {     ... Read More

Deleting all records of a collection in MongoDB Shell?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

601 Views

To delete all records of a collection in MongoDB shell, use the remove() method. The syntax is as follows −db.yourCollectionName.remove({});To understand the syntax, let us create a collection with document. The query to create a collection with document is as follows −> db.deleteAllRecordsDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f6ca32f684a30fbdfd596") } > db.deleteAllRecordsDemo.insertOne({"StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f6cb22f684a30fbdfd597") } > db.deleteAllRecordsDemo.insertOne({"StudentName":"Mike", "StudentAge":23, "Hobby":["Learning", "Photography"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f6cde2f684a30fbdfd598") }Display all documents from a collection with the help of find() method. The query is as follows −> db.deleteAllRecordsDemo.find().pretty();The following ... Read More

How to sum the value of a key across all documents in a MongoDB collection?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

4K+ Views

To get sum the value of a key across all documents in a MongoDB collection, you can use aggregate().To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.sumOfValueDemo.insertOne({"Name":"Larry", "Amount":14.50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ee7272f684a30fbdfd592") } > db.sumOfValueDemo.insertOne({"Name":"Mike", "Amount":15.68}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ee7342f684a30fbdfd593") } > db.sumOfValueDemo.insertOne({"Name":"Carol", "Amount":50.32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ee7412f684a30fbdfd594") } > db.sumOfValueDemo.insertOne({"Name":"David", "Amount":120.90}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ee7532f684a30fbdfd595") }Display all documents from a collection ... Read More

Opposite of $addToSet to '$removeFromSet' in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

To get the opposite of $addToSet to '$removeFromSet', use the $pull operator.Let us create a collection with a document. The query to create a collection with a document is as follows −> db.oppositeAddToSetDemo.insertOne({"StudentName":"John", "StudentHobby":["Cricket", "Cooking", "Drawing"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eddcc2f684a30fbdfd588") } > db.oppositeAddToSetDemo.insertOne({"StudentName":"Carol", "StudentHobby":["Cricket", "Dance", "Hiking"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8eddfd2f684a30fbdfd589") } > db.oppositeAddToSetDemo.insertOne({"StudentName":"David", "StudentHobby":["Learning", "Photography"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ede272f684a30fbdfd58a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.oppositeAddToSetDemo.find().pretty();The following is the output −{    "_id" ... Read More

Find documents with arrays not containing a document with a particular field value in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

654 Views

You can use $nin operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.documentWithAParticularFieldValueDemo.insertOne(    ... {       ...       ... "StudentId" : 101,       ... "StudentDetails" :       ... [          ... {             ... "TheoryMarks": 78,             ... "PracticalMarks": 91          ... },          ... {             ... Read More

How to return only unique values (no duplicates) in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

866 Views

You can use distinct() to return only unique values. The syntax is as follows −db.yourCollectionName.distinct("yourFieldName");To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Larry", "CustomerAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed7262f684a30fbdfd580") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Mike", "CustomerAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed72d2f684a30fbdfd581") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Sam", "CustomerAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed7322f684a30fbdfd582") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"Carol", "CustomerAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed73a2f684a30fbdfd583") } > db.returnOnlyUniqueValuesDemo.insertOne({"CusomerName":"David", "CustomerAge":22}); {    "acknowledged" : true,    "insertedId" ... Read More

Find largest document size in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

617 Views

To find the largest document size in MongoDB, you need to write a script in the shell.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.largestDocumentDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed2e32f684a30fbdfd57d") } > db.largestDocumentDemo.insertOne({"StudentName":"Carol", "StudentAge":22, "StudentCountryName":"US", "TechnicalSubject":["C", "C++", "Java", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed3282f684a30fbdfd57e") } > db.largestDocumentDemo.insertOne({"StudentName":"Mike", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ed3382f684a30fbdfd57f") }Display all documents from a collection with the help of find() method. The query is as follows −> ... Read More

How to find through list of ids in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use $in operator to find through the list of ids in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findListOfIdsDemo.insertOne({"StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecadd2f684a30fbdfd575") } > db.findListOfIdsDemo.insertOne({"StudentName":"Bob", "StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecae42f684a30fbdfd576") } > db.findListOfIdsDemo.insertOne({"StudentName":"David", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecaed2f684a30fbdfd577") } > db.findListOfIdsDemo.insertOne({"StudentName":"John", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8ecaf82f684a30fbdfd578") } > db.findListOfIdsDemo.insertOne({"StudentName":"Mike", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" ... Read More

Advertisements