- Trending Categories
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 6702 Articles for Database
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
153 Views
In order to connect to my table by command line, you need to use db commanddb.yourCollectionName.find();Let’s say we have a database “sample” with some collections. First check the current database> use sample; switched to db sample > db; Sample Now we have reached the database sample. The database “sample” is having the following collections: > show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationHere is the correct way of connecting to a table i.e. collection. You ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
318 Views
In order to handle empty data, you can use $ne operator. Let us create a collection with documents. Following is the query>db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":""}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd5ca629b87623db1b12") } >db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John", "StudentCountryName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd6ba629b87623db1b13") } > db.handlingAndEmptyDataDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cbd71a629b87623db1b14") }Following is the query to display all documents from a collection with the help of find() method> db.handlingAndEmptyDataDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9cbd5ca629b87623db1b12"), "StudentName" : "John", "StudentCountryName" : "" } { "_id" : ObjectId("5c9cbd6ba629b87623db1b13"), "StudentName" : ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
156 Views
Use use renameCollection() to change collection name in MongoDB. Following is the syntaxdb.yourOldCollectionName.renameCollection("yourNewCollectionName");Let us create a collection with documents. Following is the query> db.savingInformationDemo.insertOne({"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb44da629b87623db1b07") } > db.savingInformationDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb45da629b87623db1b08") } > db.savingInformationDemo.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb461a629b87623db1b09") } > db.savingInformationDemo.insertOne({"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cb465a629b87623db1b0a") }Following is the query to display all documents from a collection with the help of find() method> db.savingInformationDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9cb44da629b87623db1b07"), "StudentName" : "Larry" } { ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
1K+ Views
Following is the syntax to loop through collections with cursorvar anyVariableName1; var anyVariableName2= db.yourCollectionName.find(); while(yourVariableName2.hasNext()) { yourVariableName1= yourVariableName2.next(); printjson(yourVariableName1); };Let us create a collection with documents. Following is the query> db.loopThroughCollectionDemo.insertOne({"StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca81f2d6669774125247f") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8272d66697741252480") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Chris", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8462d66697741252481") } > db.loopThroughCollectionDemo.insertOne({"StudentName":"Robert", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ca8632d66697741252482") }Following is the query to display all documents from a collection with the help of find() method> db.loopThroughCollectionDemo.find().pretty();This will ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
650 Views
To remove all documents from a collection except a single document in MongoDB, use remove() based on some condition. Let us create a collection with documents. Following is the query>db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Larry", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9de42d66697741252478") } >db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Mike", "StudentAge":21, "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9dea2d66697741252479") } >db.removeAllDocumentsExceptOneDemo.insertOne({"StudentName":"Chris", "StudentAge":24, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9def2d6669774125247a") }Following is the query to display all documents from a collection with the help of find() method> db.removeAllDocumentsExceptOneDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c9de42d66697741252478"), "StudentName" : "Larry", "StudentAge" : ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
213 Views
You can use $where operator to compare field values in MongoDB. Let us first create a collection with documents> db.comparingFieldDemo.insertOne({"Value1":30, "Value2":40}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99ed2d6669774125246e") } > db.comparingFieldDemo.insertOne({"Value1":60, "Value2":70}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99f62d6669774125246f") } > db.comparingFieldDemo.insertOne({"Value1":160, "Value2":190}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c99ff2d66697741252470") } > db.comparingFieldDemo.insertOne({"Value1":200, "Value2":160}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c9a0b2d66697741252471") }Following is the query to display all documents from a collection with the help of find() method> db.comparingFieldDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c99ed2d6669774125246e"), "Value1" : 30, ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
370 Views
To find exact array match with values in different order, you can use $all operator. Let us create a collection with documents. Following is the query>db.exactMatchArrayDemo.insertOne({"StudentName":"David", "StudentAge":22, "StudentGameScores":[45, 78, 98]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94702d6669774125246c") } >db.exactMatchArrayDemo.insertOne({"StudentName":"Chris", "StudentAge":23, "StudentGameScores":[45, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94a42d6669774125246d") }Following is the query to display all documents from a collection with the help of find() method> db.exactMatchArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
147 Views
Yes, you can do that. First, you need to create an index and then use explain(). Let us first create a MongoDB index. Following is the query:> db.indexOrQueryDemo.ensureIndex({"First":1});This will produce the following output{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }The query to create second index is as follows> db.indexOrQueryDemo.ensureIndex({"Second":1});This will produce the following output{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 3, "numIndexesAfter" : 4, "ok" : 1 }Following is the query for $or operator with indexes. We have used explain() as well here> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain();This will produce ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
1K+ Views
Following is the syntax to retrieve the documents whose values end with a particular character in MongoDBdb.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();Let us first create a collection with documents>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam", "StudentAge":25, "StudentCountryName":"LAOS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45b32d66697741252456") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam", "StudentAge":24, "StudentCountryName":"ANGOLA"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45c02d66697741252457") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert", "StudentAge":21, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45cb2d66697741252458") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris", "StudentAge":20, "StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45d92d66697741252459") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c45eb2d6669774125245a") }Following is the query to display all documents from a collection ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
2K+ Views
Use $unwind operator with $project for to get the first element in an array. Let us create a collection with documents. Following is the query>db.getFirstElementInArrayDemo.insertOne({"StudentName":"John", "StudentSubject":["MongoDB", "Python", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c41292d6669774125244e") } >db.getFirstElementInArrayDemo.insertOne({"StudentName":"Chris", "StudentSubject":["Java", "C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c413f2d6669774125244f") } >db.getFirstElementInArrayDemo.insertOne({"StudentName":"Robert", "StudentSubject":["C++", "Ruby"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c41532d66697741252450") }Following is the query to display all documents from a collection with the help of find() method> db.getFirstElementInArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9c41292d6669774125244e"), "StudentName" : "John", "StudentSubject" : [ ... Read More