Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Big Data Analytics Articles - Page 125 of 167
173 Views
You can use $in operator. Let us first create a collection with documents −> db.tagCountDemo.insertOne({"ListOfNames":["John", "Sam", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob", "David", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike", "Robert", "Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James", "Carol", "Jace"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }Following is the query to display all documents from a collection with the help of find() method −> db.tagCountDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd64b387924bb85b3f48944"), "ListOfNames" : ... Read More
329 Views
To sort by sub-document match, you can use aggregate framework. Let us first create a collection with documents −> db.sortBySubDocumentsDemo.insertOne( { "StudentName": "Chris", "StudentDetails": [ { "Age":21, "StudentScore":91 }, { "Age":22, "StudentScore":99 }, { "Age":21, "StudentScore":93 ... Read More
331 Views
You can use $lte operator along with Date() for this. Let us first create a collection with documents. Here, we have set the date 2019-05-11, which is the current date −> db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-11")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563b17924bb85b3f4893b") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-01-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563bf7924bb85b3f4893c") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563ca7924bb85b3f4893d") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563e77924bb85b3f4893e") }Following is the query to display all documents from a collection with the help of find() method −> db.getDocumentsExpiredDemo.find().pretty();This ... Read More
400 Views
To update array in MongoDB document by variable index, use the below syntax. Here, yourIndexValue in the index value, where yourIndexVariableName is the variable name for index −var yourIndexVariableName= yourIndexValue, anyVariableName= { "$set": {} }; yourVariableName["$set"]["yourFieldName."+yourIndexVariableName] = "yourValue"; db.yourCollectionName.update({ "_id": yourObjectId}, yourVariableName);Let us first create a collection with documents −> db.updateByVariableDemo.insertOne({"StudentSubjects":["MySQL", "Java", "SQL Server", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd553c37924bb85b3f4893a") }Following is the query to display all documents from a collection with the help of find() method −> db.updateByVariableDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd553c37924bb85b3f4893a"), "StudentSubjects" : [ ... Read More
168 Views
You can use map() for this. Let us first create a collection with documents −> db.deleteDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550492cba06f46efe9f06") } > db.deleteDemo.insertOne({"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5504d2cba06f46efe9f07") } > db.deleteDemo.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550512cba06f46efe9f08") } > db.deleteDemo.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5505d2cba06f46efe9f09") } > db.deleteDemo.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550682cba06f46efe9f0a") } > db.deleteDemo.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5506f2cba06f46efe9f0b") } > db.deleteDemo.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd550752cba06f46efe9f0c") } > db.deleteDemo.insertOne({"Name":"Bob"}); ... Read More
490 Views
You can use $and operator. Let us first create a collection with documents −>db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"John", "StudentAge":23, "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd547142cba06f46efe9f02") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Carol", "StudentAge":21, "StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5471f2cba06f46efe9f03") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"David", "StudentAge":24, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5472c2cba06f46efe9f04") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Robert", "StudentAge":26, "StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd548382cba06f46efe9f05") }Following is the query to display all documents from a collection with the help of find() method −> db.selectingASingleFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd547142cba06f46efe9f02"), "StudentFirstName" : "John", "StudentAge" : ... Read More
317 Views
You can use aggregate framework for this. Let us first create a collection with documents −>db.aggregationOperatorDemo.insertOne({"FirstValue":392883,"SecondValue":10000000000}); { "acknowledged" : true, "insertedId" : ObjectId("5cd541452cba06f46efe9f01") }Following is the query to display all documents from a collection with the help of find() method −> db.aggregationOperatorDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd541452cba06f46efe9f01"), "FirstValue" : 392883, "SecondValue" : 10000000000 }Following is the query to use divide aggregation operator −> db.aggregationOperatorDemo.aggregate([ ... { "$project": { "Value": { "$divide": ["$FirstValue", "$SecondValue"] } } } ... ]);This will produce the following output −{ "_id" : ObjectId("5cd541452cba06f46efe9f01"), "Value" : 0.0000392883 }
240 Views
Let us first create a collection with documents −> db.searchAnArrayDemo.insertOne({_id:1, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 1 } > db.searchAnArrayDemo.insertOne({_id:2, "TechnicalDetails":[{"Language":"MySQL"}]}); { "acknowledged" : true, "insertedId" : 2 } > db.searchAnArrayDemo.insertOne({_id:3, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 3 } > db.searchAnArrayDemo.insertOne({_id:4, "TechnicalDetails":[{"Language":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : 4 } > db.searchAnArrayDemo.insertOne({_id:5, "TechnicalDetails":[{"Language":"Java"}]}); { "acknowledged" : true, "insertedId" : 5 }Following is the query to display all documents from a collection with the help of find() method −> db.searchAnArrayDemo.find().pretty();This will produce the following output −{ "_id" : 1, "TechnicalDetails" : [ { "Language" : "MongoDB" } ] } ... Read More
185 Views
To update the inner field, use the below syntax −db.yourCollectionName.update({"_id" : yourObjectId}, {$set : {"yourOuterFieldName.yourInnerFieldName" :yourValue}});Let us first create a collection with documents −> db.updateDocumentDemo.insertOne( ... { ... ... "StudentDetails" : { ... "StudentFirstName" : "Adam", ... "StudentLastName" : "Samith" ... }, ... "StudentOtherDetails" : { ... "StudentFavouriteSubject" : "MySQL", ... "StudentScore" : 45 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd50bb32cba06f46efe9efe") }Following is the query to ... Read More
95 Views
You can use $set operator along with update(). Let us first create a collection with documents −> db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd506fe2cba06f46efe9efa") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507022cba06f46efe9efb") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507022cba06f46efe9efc") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507032cba06f46efe9efd") }Following is the query to display all documents from a collection with the help of find() method −> db.workingOfUpdateMethod.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd506fe2cba06f46efe9efa"), "ClientCountryName" ... Read More