
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

113 Views
Use $addToSet to create a new field in MongoDB. Let us first create a collection with documents −> db.createFieldDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e28b50a6c6dd317ad95") } > db.createFieldDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e2fb50a6c6dd317ad96") } > db.createFieldDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e38b50a6c6dd317ad97") } > db.createFieldDemo.insertOne({"StudentFirstName":"David", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e43b50a6c6dd317ad98") }Following is the query to display all documents from a collection with the help of find() method −> db.createFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd99e28b50a6c6dd317ad95"), "StudentFirstName" : ... Read More

1K+ Views
To store MongoDB result in an array, use the toArray() method −var anyVariableName=db.yourCollectionName.find().toArray();Let us first create a collection with documents −> db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":24, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bd5b50a6c6dd317ad92") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Sam Williams", "CustomerAge":46, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99beab50a6c6dd317ad93") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Carol Taylor", "CustomerAge":23, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bf9b50a6c6dd317ad94") }Following is the query to display all documents from a collection with the help of find() method −> db.mongoDbResultInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd99bd5b50a6c6dd317ad92"), "CustomerName" : "David Miller", "CustomerAge" ... Read More

3K+ Views
You can use the concept of Map Reduce. Let us first create a collection with documents −> db.getAllFieldNamesDemo.insertOne({"StudentFirstName":"David", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd998e9b50a6c6dd317ad90") }Following is the query to display all documents from a collection with the help of find() method −> db.getAllFieldNamesDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd998e9b50a6c6dd317ad90"), "StudentFirstName" : "David", "StudentAge" : 23 }Following is the query to get all fields names in a MongoDB collection −> myMapReduce = db.runCommand({ "mapreduce" : "getAllFieldNamesDemo", "map" : function() { for (var myKey in this) { emit(myKey, null); } ... Read More

511 Views
To update all elements in an array with a prefix string, use forEach(). Let us first create a collection with documents −> db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "John", "Carol" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd91908b50a6c6dd317ad8e") } > > > db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "Sam" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd9191cb50a6c6dd317ad8f") }Following is the query to display all documents from ... Read More

233 Views
Use the $pull to remove array elements from a MongoDB document as shown in the following syntax −db.yourCollectionName.update( { }, { $pull: { yourFieldName: yourValue }}, {multi:true });Let us first create a collection with documents −>db.removeArrayElementsDemo.insertOne({"AllPlayerName":["John", "Sam", "Carol", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d011a844af18acdffc1") } >db.removeArrayElementsDemo.insertOne({"AllPlayerName":["Chris", "Robert", "John", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d2e1a844af18acdffc2") }Following is the query to display all documents from a collection with the help of find() method −> db.removeArrayElementsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd90d011a844af18acdffc1"), "AllPlayerName" : [ "John", ... Read More

227 Views
You can use $exists operator for this. Let us first create a collection with documents −>db.checkFieldExistsDemo.insertOne({"StudentFirstName":"John", "StudentGender":"Male", "StudentMongoDBScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909611a844af18acdffbd") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Emma", "StudentGender":"Female", "StudentMongoDBScore":58}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909781a844af18acdffbe") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Carol", "StudentGender":"Male", "StudentMongoDBScore":77}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909871a844af18acdffbf") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"David", "StudentMongoDBScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909a31a844af18acdffc0") }Following is the query to display all documents from a collection with the help of find() method −> db.checkFieldExistsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd909611a844af18acdffbd"), "StudentFirstName" : "John", "StudentGender" ... Read More

154 Views
Yes, we can avoid the _id, using the following syntax in MongoDB −db.yourCollectionName.find({}, { _id:0});Let us first create a collection with documents:>> db.excludeIdDemo.insertOne({"CustomerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f62c1a844af18acdffb9") } > db.excludeIdDemo.insertOne({"CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6311a844af18acdffba") } > db.excludeIdDemo.insertOne({"CustomerName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6351a844af18acdffbb") } > db.excludeIdDemo.insertOne({"CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6381a844af18acdffbc") }Following is the query to display all documents from a collection with the help of find() method −> db.excludeIdDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd7f62c1a844af18acdffb9"), "CustomerName" : "Larry" } ... Read More

1K+ Views
Use $set to replace value in an array. Let us first create a collection with documents −> db.replaceValueInArrayDemo.insertOne({"StudentScores":[45, 56, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0421a844af18acdffb7") } > db.replaceValueInArrayDemo.insertOne({"StudentScores":[33, 90, 67]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0521a844af18acdffb8") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceValueInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd7f0421a844af18acdffb7"), "StudentScores" : [ 45, 56, 78 ] } { "_id" : ObjectId("5cd7f0521a844af18acdffb8"), "StudentScores" : [ ... Read More

958 Views
Let us first create a collection with documents −> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"JOHN Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7edef1a844af18acdffb2") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"SAM Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee011a844af18acdffb3") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"CAROL Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee101a844af18acdffb4") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"Bob Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee351a844af18acdffb5") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"DAVID Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee451a844af18acdffb6") }Following is the query to display all documents from a collection with the help of find() method −> db.upperCaseFiveLetterDemo.find().pretty();This will produce the following output −{ "_id" : ... Read More

120 Views
The $gt is for greater than, wherein select those documents where the value of the field is greater than the specified value. Let us first create a collection with documents −> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000, "PlayerLevel":2}, "PlayerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0, "PlayerLevel":1}, "PlayerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10, "PlayerLevel":0}, "PlayerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1, "PlayerLevel":1}, "PlayerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }Following is the query to display all documents from a collection with the help of ... Read More