Found 1661 Articles for Big Data Analytics

Iterate a cursor and print a document in MongoDB?

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

276 Views

For this, use printjson. Let us first create a collection with documents −> db.cursorDemo.insertOne({"StudentFullName":"John Smith", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442") } > db.cursorDemo.insertOne({"StudentFullName":"John Doe", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443") } > db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444") } > db.cursorDemo.insertOne({"StudentFullName":"Chris Brown", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445") }Following is the query to display all documents from a collection with the help of find() method −> db.cursorDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"),    "StudentFullName" : "John ... Read More

Find all collections in MongoDB with specific field?

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

1K+ Views

Let us implement the above syntax in order to find all documents in MongoDB with field name “StudentFirstName”. The query is as follows −> db.getCollectionNames().forEach(function(myCollectionName) { ...    var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count(); ...    if (frequency > 0) { ...       print(myCollectionName); ...    } ... });This will produce the following output −multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemoLet us verify the removeKeyFieldsDemo collection has field with name “StudentFirstName” or not. Following is the query −> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}});This will produce the following output displaying the StudentFirstName field exist −{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" ... Read More

Pull multiple objects from an array in MongoDB?

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

1K+ Views

To pull multiple objects from an array, you can use $pull operator. Let us first create a collection with documents −> db.pullMultipleObjectsDemo.insertOne( ...    { ...       "ClientId" : "100", ...       "ClientName" : "John", ...       "ClientPersonalDetails" : [ ...          { ...             "ClientCountryName" : "US", ...             "ClientProjectName" : "Online Book Store", ... ...          }, ...          { ...             "ClientCountryName" : "AUS", ...     ... Read More

How do I change a MongoDB user's password?

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

1K+ Views

You need to use changeUserPassword() to change a user’s password. Let us first create a user with some roles. Following is the query to create a user in MongoDB −> use admin switched to db admin > db.createUser( ...    { ...       user: "Chris", ...       pwd: "chris", ...       roles: [ { role: "readWrite", db: "test" } ] ...    } ... ); Successfully added user: {    "user" : "Chris",    "roles" : [       {          "role" : "readWrite",          "db" : ... Read More

Find in a dictionary like structure by value with MongoDB?

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

1K+ Views

You can use find() for this. Let us first create a collection with documents −> db.findInDictionaryDemo.insertOne( ...    { ...       "_id":101, ...       "AllCustomerDetails": ...       { ...          "SomeCustomerDetail1": ...          { ...             "CustomerName1":"John Doe", ...             "CustomerName2":"John Smith" ...          }, ...          "SomeCustomerDetail2": ...          { ...             "CustomerName1":"Carol Taylor", ...             "CustomerName2":"David Miller" ... Read More

“Toggle” query in MongoDB?

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

413 Views

You need to find the document and after that you need to use update to toggle query. Let us first create a collection with documents −> db.toggleDemo.insertOne({"CustomerName":"John Smith", "CustomerAge":28, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7be138f9e6ff3eb0ce43b") } > db.toggleDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":25, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7be2e8f9e6ff3eb0ce43c") }Following is the query to display all documents from a collection with the help of find() method −> db.toggleDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc7be138f9e6ff3eb0ce43b"),    "CustomerName" : "John Smith",    "CustomerAge" : 28,    "isMarried" : true } {    "_id" : ObjectId("5cc7be2e8f9e6ff3eb0ce43c"),    "CustomerName" : "David Miller",    "CustomerAge" : 25, ... Read More

Regex to ignore a specific character in MongoDB?

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

1K+ Views

You can use regular expression along with $not operator to ignore a specific character and display rest of them. Let us first create a collection with documents −> db.regexDemo.insertOne({"CustomerId":"Customer#1234", "CustomerName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7428f8f9e6ff3eb0ce436") } > db.regexDemo.insertOne({"CustomerId":"Customer5678", "CustomerName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7429e8f9e6ff3eb0ce437") } > db.regexDemo.insertOne({"CustomerId":"Customer#777", "CustomerName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc742ae8f9e6ff3eb0ce438") } > db.regexDemo.insertOne({"CustomerId":"Customer777", "CustomerName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc742bc8f9e6ff3eb0ce439") }Following is the query to display all documents from a collection with the help of find() method −> db.regexDemo.find().pretty();This will produce the ... Read More

How do you perform an AND query on an array in MongoDB?

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

189 Views

To get the same result like AND in MongoDB, use the $all operator. Let us first create a collection with documents −> db.andQueryDemo.insertOne({"StudentName":"Carol Taylor", "FavouriteSubject":["C", "Java", "MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73e7a8f9e6ff3eb0ce433") } > db.andQueryDemo.insertOne({"StudentName":"David Miller", "FavouriteSubject":["C++", "Java", "MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73ea48f9e6ff3eb0ce434") } > db.andQueryDemo.insertOne({"StudentName":"Carol Taylor", "FavouriteSubject":["Python", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73ed38f9e6ff3eb0ce435") }Following is the query to display all documents from a collection with the help of find() method −> db.andQueryDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc73e7a8f9e6ff3eb0ce433"),   ... Read More

How to sum every field in a sub document of MongoDB?

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

415 Views

To sum every field in a sub document, use aggregate framework. Let us first create a collection with documents −> db.sumEveryFieldDemo.insertOne( ...    { ...       "_id":101, ...       "PlayerDetails": [ ...          {"PlayerName":"John", "PlayerScore":1000}, ...          {"PlayerName":"Carol", "PlayerScore":2000}, ...          {"PlayerName":"Sam", "PlayerScore":3000} ...       ] ...    } ... ); { "acknowledged" : true,  "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method −> db.sumEveryFieldDemo.find().pretty();This will produce the following output −{    "_id" : 101,    "PlayerDetails" : [       {          "PlayerName" : "John",   ... Read More

Check if value exists for a field in a MongoDB document?

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

2K+ Views

To check if value exists for a field in a MongoDB document, you can use find() along with $exists operator. Let us first create a collection with documents −> db.checkIfValueDemo.insertOne({"PlayerName":"John Smith", "PlayerScores":[5000, 98595858, 554343]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f507af8e7a4ca6b2ad98") } > db.checkIfValueDemo.insertOne({"PlayerName":"John Doe", "PlayerScores":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f512af8e7a4ca6b2ad99") } > db.checkIfValueDemo.insertOne({"PlayerName":"Carol Taylor", "PlayerScores":[7848474, 8746345353]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f521af8e7a4ca6b2ad9a") } > db.checkIfValueDemo.insertOne({"PlayerName":"David Miller", "PlayerScores":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f531af8e7a4ca6b2ad9b") }Following is the query to display all documents from a collection with the help ... Read More

Advertisements