Found 1661 Articles for Big Data Analytics

List all values of a certain field in MongoDB?

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

2K+ Views

To get the list of all values of certain fields in MongoDB, you can use distinct(). The syntax is as follows −db.yourCollectionName.distinct( "yourFieldName");To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc89ed3c9d04998abf011") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8abd3c9d04998abf012") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fc8d7d3c9d04998abf013") } > db.listAllValuesOfCeratinFieldsDemo.insertOne({"ListOfValues":[40, 50, 70]}); {    "acknowledged" : true,    "insertedId" ... Read More

How to create a nested index in MongoDB?

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

466 Views

To create nested index in MongoDB, you can use createIndex() or ensureIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourOuterFieldName.yourInnerFieldName.yourSecondInnerFieldName": 1});To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.nestedIndexDemo.insertOne(    ... {       ...       ... "CustomerId":101,       ... "CustomerDetails":       ... {          ... "CustomerListDetails":          ... {             ... "CustomerName":"Larry",             ... "CustomerProjectName": "Project-1",           ... Read More

Delete all elements in an array field in MongoDB?

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

2K+ Views

You can use $set operator for this. The syntax is as follows −db.yourCollectionName.update({}, { $set : {"yourFieldName": [] }} , {multi:true} );To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Larry", "InstructorTechnicalSubject":["Java", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fb971d3c9d04998abf00e") } > db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Mike", "InstructorTechnicalSubject":["C", "C++", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8fb98ad3c9d04998abf00f") }Display all documents from a collection with the help of find() method. The query is as follows −> db.deleteAllElementsInArrayDemo.find().pretty();The following is the output −{    "_id" ... Read More

How to sort in MongoDB?

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

179 Views

To sort in MongoDB, you can use the sort() method.Case 1 − Sort in ascending order. The syntax is as follows −db.yourCollectionName.find().sort({yourField:1});Case 2 − Sort in descending order. The syntax is as follows −db.yourCollectionName.find().sort({yourField:-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.sortingDemo.insertOne({"Value":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); {    "acknowledged" : true, ... Read More

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

367 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

13K+ 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

701 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

5K+ 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

Advertisements