- 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 1659 Articles for Big Data Analytics
82 Views
To improve querying field in MongoDB, you need to use index. Let us create a collection with documents> db.improveQueryDemo.insertOne( ... { ... "PlayerDetails":[ ... {"PlayerName": "John", "PlayerGameScore": 5690}, ... {"PlayerName": "Carol", "PlayerGameScore": 2690}, ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9dbaf875e2eeda1d5c3670") }Following is the query to display all documents from a collection with the help of find() method> db.improveQueryDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9dbaf875e2eeda1d5c3670"), "PlayerDetails" : [ { "PlayerName" : "John", ... Read More
2K+ Views
In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB. The MongoDB equivalent syntax is as followsdb.yourCollectionName.aggregate( [ { "$project": { "_id": 0, "anyAliasName": "$yourFieldName" }} ]);Let us first create a collection with documents> db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d448827b86948e204ca91") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449027b86948e204ca92") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449527b86948e204ca93") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449927b86948e204ca94") ... Read More
710 Views
To get the last element of array in MongoDB, use the following syntaxdb.yourCollectionName.find({}, {yourArrayFieldName:{$slice:-1}});Let us first create a collection with documents>db.getLastElementOfArrayDemo.insertOne({"StudentName":"James", "StudentMathScore":[78, 68, 98]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d71a629b87623db1b2e") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Chris", "StudentMathScore":[88, 56, 34]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d83a629b87623db1b2f") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Larry", "StudentMathScore":[99]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2d8ea629b87623db1b30") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Robert", "StudentMathScore":[90, 78, 67, 66, 75, 73]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2dada629b87623db1b31") }Following is the query to display all documents from a collection with the help of find() method> db.getLastElementOfArrayDemo.find().pretty();This will produce the following output{ ... Read More
2K+ Views
Yes, to retrieve multiple docs from MongoDB by id, use the $in operator. The syntax is as followsdb.yourCollectionName.find({_id:{$in:[yourValue1, yourValue2, yourValue3, ...N]}});Let us first create a collection with documents:> db.retrieveMultipleDocsByIdDemo.insertOne({"_id":10, "CustomerName":"John"}); { "acknowledged" : true, "insertedId" : 10 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":14, "CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : 14 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":20, "CustomerName":"Robert"}); { "acknowledged" : true, "insertedId" : 20 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":25, "CustomerName":"Sam"}); { "acknowledged" : true, "insertedId" : 25 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":30, "CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : 30 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":34, "CustomerName":"Carol"}); { "acknowledged" : true, "insertedId" : 34 }Following is the query to display all documents ... Read More
434 Views
To insert a specific index for MongoDB array, you can use $push operator. Let us create a collection with documents>db.insertToSpecificIndexDemo.insertOne({"StudentName":"Larry", "StudentSubjects":["MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2562a629b87623db1b2c") } >db.insertToSpecificIndexDemo.insertOne({"StudentName":"Chris", "StudentSubjects":["C++", "C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d2573a629b87623db1b2d") }Following is the query to display all documents from a collection with the help of find() method> db.insertToSpecificIndexDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9d2562a629b87623db1b2c"), "StudentName" : "Larry", "StudentSubjects" : [ "MySQL", "Java" ] } { "_id" : ObjectId("5c9d2573a629b87623db1b2d"), "StudentName" : "Chris", ... Read More
7K+ Views
To set a query for not equal to null or empty, use the $nin operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$nin:[null, ""]}});Let us create a collection with documents> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Larry", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20b6a629b87623db1b26") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20bea629b87623db1b27") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Sam", "UserAge":32}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20c7a629b87623db1b28") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":null, "UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20d2a629b87623db1b29") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Robert", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d20dda629b87623db1b2a") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":23}); { "acknowledged" : true, ... Read More
70 Views
To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. Following is the syntaxdb.yourCollectionName.find({yourFieldName:{$exists:true}});Let us create a collection with documents>db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"John", "StudentAge":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d1d60a629b87623db1b22") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Larry", "StudentAge":null}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d1d70a629b87623db1b23") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":""}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d1d7ba629b87623db1b24") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":""}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d1d81a629b87623db1b25") }Following is the query to display all documents from a collection with the help of find() method> db.findAllDocumentWhichHaveFieldDemo.find().pretty();This will produce the following ... Read More
1K+ Views
In order to insert Date() in MongoDB through Mongo shell, use the following syntaxvar yourVariableName= new Date(year, month, day, hour, minute); db.yourCollectionName({yourDateFieldName:yourVariableName});Let us first create a date variable> var creatingDate = new Date(2019, 03, 29, 13, 12);Let us create a collection with documents:>db.insertingDateUsingVariableDemo.insertOne({"UserName":"John", "UserMessages":["Hi", "Hello", "Awesome"], "UserPostDate":creatingDate});Following is the query to display all documents from a collection with the help of find() method> db.insertingDateUsingVariableDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9d1b19a629b87623db1b21"), "UserName" : "John", "UserMessages" : [ "Hi", "Hello", "Awesome" ], "UserPostDate" : ISODate("2019-04-29T07:42:00Z") }Read More
442 Views
To remove all collections whose name matches a string, you can follow some steps. Use for loop to iterate over all collections and find that particular collection name with certain string. After that, use the drop method to remove all collections.Let’s say we are using the database “sample”. The collections are as follows in sample database> show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteMultipleIdsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findMimimumElementInArrayDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo insertDocumentWithDateDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationNow remove all the collection names that match a ... Read More
1K+ Views
To delete multiple ids in MongoDB, you can use $in operator. Following is the syntaxdb.yourCollectionName.remove( { _id : { $in: [yourObjectId1, yourObjectId2, yourObjectId3)] } } );Let us create a collection with documents> db.deleteMultipleIdsDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cd7d6a629b87623db1b19") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Robert", "ClientAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cd7dea629b87623db1b1a") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Sam", "ClientAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cd7e9a629b87623db1b1b") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"John", "ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cd7f7a629b87623db1b1c") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Carol", "ClientAge":36}); { "acknowledged" : true, "insertedId" : ObjectId("5c9cd803a629b87623db1b1d") }Following is the query to ... Read More