
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
Found 1661 Articles for Big Data Analytics

536 Views
You can use $elemMatch operator for this. Let us create a collection with documents> db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-1", "Tag-2", "Tag-3"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb4d5d628fa4220163b79") } > db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-2", "Tag-4", "Tag-5"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb4d5d628fa4220163b7a") } > db.getDocumentsByTagsDemo.insertOne({"Tags":["Tag-6", "Tag-4", "Tag-3"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb4d6d628fa4220163b7b") }Following is the query to display all documents from a collection with the help of find() method> db.getDocumentsByTagsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9eb4d5d628fa4220163b79"), "Tags" : [ "Tag-1", "Tag-2", "Tag-3" ] } ... Read More

171 Views
You need to use $exists operator to determine whether a field exists in MongoDB. Let us first create a collection with documents> db.determineFieldExistsDemo.insertOne({"ClientName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb245d628fa4220163b75") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Larry", "ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb25cd628fa4220163b76") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Mike", "ClientCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb26fd628fa4220163b77") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Sam", "ClientAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb286d628fa4220163b78") }Following is the query to display all documents from a collection with the help of find() method> db.determineFieldExistsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9eb245d628fa4220163b75"), "ClientName" : "John" ... Read More

821 Views
To get connected clients in MongoDB, use currentOp() with the set value to true and you need to iterate array result set with the help of field client. Let us first implement currentOp> db.currentOp(true)Following is the output. Here the client is 127.0.0.1 since we are using localhost. The output displays all the connected clients{ "inprog" : [ { "host" : "DESKTOP-QN2RB3H:27017", "desc" : "conn1", "connectionId" : 1, "client" : "127.0.0.1:61787", "appName" : "MongoDB Shell", ... Read More

799 Views
You can use $slice operator to limit an array. Let us create a collection with documents. Following is the query> db.limitAnArrayDemo.insertOne( ... { ... _id: 101, ... "PlayerName": "Bob", ... "PlayerDetails": {Age:23, isStudent:true}, ... "PlayerGameScores": [234, 5767, 58, 67, 78, 90, 1002, 576, 68, 45, 23, 45, 678, 89, 78 ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method> db.limitAnArrayDemo.find().pretty();This will produce the following output{ ... Read More

805 Views
To update only specific field, you can use $set operator. Let us first create a collection with documents>db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"John", "EmployeeCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea849d628fa4220163b72") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"Larry", "EmployeeCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea853d628fa4220163b73") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"David", "EmployeeCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea85bd628fa4220163b74") }Following is the query to display all documents from a collection with the help of find() method> db.updateOnlySpecificFieldDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9ea849d628fa4220163b72"), "EmployeeName" : "John", "EmployeeCountryName" : "UK" } { "_id" : ObjectId("5c9ea853d628fa4220163b73"), "EmployeeName" : "Larry", "EmployeeCountryName" : ... Read More

980 Views
In order to return only value of a field in MongoDB, you need to write a query and use forEach loop. Let us first create a collection with documents> db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea537d628fa4220163b6e") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea53bd628fa4220163b6f") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea541d628fa4220163b70") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Ramit"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea549d628fa4220163b71") }Following is the query to display all documents from a collection with the help of find() method> db.returnOnlyValueOfFieldDemo.find().pretty();This will produce the following output{ "_id" : ... Read More

218 Views
You can use dot notation to get nested value. Let us first create a collection with documents> db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "John", ... "EmployeeDetails" : ... { ... ... "_id":"EMP-101", ... "EmployeeAge":23, ... "EmployeeCompanyName":"IBM" ... ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea31dd628fa4220163b69") } > db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "Carol", ... "EmployeeDetails" : ... ... Read More

241 Views
To get component of Date/ISODate in MongoDB, let us create a document with date in the collection. Now let us get the component of Date/ISODate in MongoDB> db.componentOfDateDemo.insert({"ShippingDate":new Date()}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method> db.componentOfDateDemo.find().pretty()This will produce the following output{ "_id" : ObjectId("5c9e9d57d628fa4220163b68"), "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") }Following is the query to get result using findOne()> var result=db.componentOfDateDemo.findOne();Now you can display documents from the collection. Following is the query> resultThis will produce the following output{ "_id" : ObjectId("5c9e9d57d628fa4220163b68"), "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") ... Read More

621 Views
To find the document by field name with a specific value, you can use $exists operator. Let us create a collection with documents> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9e93b2d628fa4220163b64") } > db.findByFieldName.insertOne({ ... " Client":{ ... " ClientDetails":{ ... " ClientName":"Chris", ... " ClientAge":27 ... }, ... "ClientEducationDetails":{ ... " isEducated":true, ... "CollegeName":"M.I.T." ... ... ... Read More

3K+ Views
There are two possibilities to check if MongoDB database exists.Case 1: The first possibility is that the MongoDB database exists i.e. it returns particular index.Case 2: The second possibility is that the MongoDB database does not exist i.e. it returns index -1.NOTE: An index starts from 0 and ends with (N-1) like an array.The syntax is as follows to check if MongoDB database exists.db.getMongo().getDBNames().indexOf("yourDatabaseName");Case 1: Let us implement the above syntax to check if MongoDB database exists. Following is the querydb.getMongo().getDBNames().indexOf("test");This will produce the following output6Look at the above sample output, we are getting 6 that means the database “test” ... Read More