- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1359 Articles for MongoDB
112 Views
You can use $where 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.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"John"}, "NewStudentDetails":{"StudentName":"Carol"}}); { "acknowledged" : true, "insertedId" : ObjectId("5c90096ed3c9d04998abf017") } > db.queryInSameDocumentsDemo.insertOne({"StudentDetails":{"StudentName":"Bob"}, "NewStudentDetails":{"StudentName":"Bob"}}); { "acknowledged" : true, "insertedId" : ObjectId("5c900a435705caea966c5573") }Display all documents from a collection with the help of find() method. The query is as follows −> db.queryInSameDocumentsDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c90096ed3c9d04998abf017"), "StudentDetails" : { "StudentName" : "John" }, "NewStudentDetails" : { ... Read More
1K+ Views
The syntax is as follows for this −db.yourCollectionName.update({ }, { $set: { "yourOuterFieldName.yourInnerFieldName": "yourValue" } });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.updateNestedValueDemo.insertOne({"CustomerName":"Chris", ... "CustomerDetails":{"CustomerAge":25, "CustomerCompanyName":"Google", "CustomerCityName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fccc4d3c9d04998abf015") }Display all documents from a collection with the help of find() method. The query is as follows −> db.updateNestedValueDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c8fccc4d3c9d04998abf015"), "CustomerName" : "Chris", "CustomerDetails" : { "CustomerAge" : 25, ... Read More
1K+ 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
352 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
1K+ 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
118 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
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
264 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
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
584 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
To Continue Learning Please Login
Login with Google