Found 1659 Articles for Big Data Analytics

Get all fields names in a MongoDB collection?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

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

How can I update all elements in an array with a prefix string?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

383 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

MongoDB query to remove array elements from a document?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

165 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

Check whether field exist in MongoDB or not?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

164 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

Can MongoDB find() function display avoiding _id?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

98 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

MongoDB query to replace value in an array?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

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

MongoDB regex to display records whose first five letters are uppercase?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

806 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

How to perform $gt on a hash in a MongoDB document?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

70 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

Check for null in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

499 Views

We will use the Null type here. Following are the null types with the alias −TypeNumberAliasDouble1“double”String2“string”Object3“object”Array4“array”Binary data5“binData”Undefined6“undefined”ObjectId7“objectId”Boolean8“bool”Date9“date”Null10“null”Regular Expression11“regex”Following is the syntax for type 10 i.e. null −db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });The above syntax will find only those documents which have null value. Let us first create a collection with documents −> db.mongoDbEqualDemo.insertOne({"Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); ... Read More

How can I drop a collection in MongoDB with two dashes in the name?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

336 Views

Let us first see the syntax to drop a collection −db.getCollection("yourCollectionNameWithTwoDashes").drop();For demo, we will create a collection name with two dashes as shown below −> db.createCollection("company--EmployeeInformation"); { "ok" : 1 }Create the above collection “company--EmployeeInformation “ with documents. Following is the query:>db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Amazon", "EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c5ff6d78f205348bc654") } >db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Google", "EmployeeName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c60b6d78f205348bc655") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection("company--EmployeeInformation").find();This will produce the following output −{ "_id" : ObjectId("5cd7c5ff6d78f205348bc654"), "CompanyName" : "Amazon", "EmployeeName" : "Chris" } { ... Read More

Advertisements