Found 1659 Articles for Big Data Analytics

Get the first element in an array and return using MongoDB Aggregate?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

2K+ Views

Use $unwind operator with $project for to get the first element in an array. Let us create a collection with documents. Following is the query>db.getFirstElementInArrayDemo.insertOne({"StudentName":"John", "StudentSubject":["MongoDB", "Python", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c41292d6669774125244e") } >db.getFirstElementInArrayDemo.insertOne({"StudentName":"Chris", "StudentSubject":["Java", "C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c413f2d6669774125244f") } >db.getFirstElementInArrayDemo.insertOne({"StudentName":"Robert", "StudentSubject":["C++", "Ruby"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c41532d66697741252450") }Following is the query to display all documents from a collection with the help of find() method> db.getFirstElementInArrayDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9c41292d6669774125244e"),    "StudentName" : "John",    "StudentSubject" : [     ... Read More

How to reduce MongoDB storage space after deleting large amount of data?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

429 Views

To reduce MongoDB storage space after deleting a large amount of data, you need to use repairDatabase(). Let’s say we are using the database “test”. Following is the query to get to the current database> db;This will produce the following outputTestAfter deleting large amount of data, this is how you can reduce the storage space of MongoDB> db.repairDatabase()This will produce the following output{ "ok" : 1 }Here is the screenshot after we implement the above query. This will reduce the storage space:

Concatenate strings from two fields into a third field in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

824 Views

To concatenate strings from two fields into a third field, you can use the following syntaxdb.yourCollectionName.aggregate(    [       { $project: { "yourNewFieldName": { $concat: [ "$yourFieldName1", " yourDellimiterValue ", "$yourFieldName2" ] } } }    ] );Let us first create a collection with documents>db.concatenateStringsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb7362d66697741252444") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb7402d66697741252445") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb74c2d66697741252446") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb7752d66697741252447") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"James", "StudentLastName":"Williams"}); {    "acknowledged" : ... Read More

Can we remove _id from MongoDB query result?

George John
Updated on 30-Jul-2019 22:30:25

5K+ Views

To remove _id from MongoDB result, you need to set 0 for _id field. Following is the syntaxdb.yourCollectionName.find({}, {_id:0});To understand it, let us create a collection with documents. Following is the query> db.removeIdDemo.insertOne({"UserName":"John", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb4042d66697741252440") } > db.removeIdDemo.insertOne({"UserName":"Mike", "UserAge":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb40c2d66697741252441") } > db.removeIdDemo.insertOne({"UserName":"Sam", "UserAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb4162d66697741252442") } > db.removeIdDemo.insertOne({"UserName":"Carol", "UserAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9bb4222d66697741252443") }Following is the query to display all documents from a collection with the help of find() method> db.removeIdDemo.find().pretty();This ... Read More

How to use MongoDB $pull to delete documents within an Array?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

182 Views

You need to use update command along with $pull operator to delete documents within an array. Let us create a collection with documents. Following is the query> db.deleteDocumentsDemo.insertOne( ... { ...    "_id":100, ...    "StudentsDetails" : [ ...       { ...          "StudentId" : 1, ...          "StudentName" : "John" ...       }, ...       { ...          "StudentId" : 2, ...          "StudentName" : "Carol" ...       }, ...       { ...         ... Read More

How to change the password in MongoDB for existing user?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

700 Views

To change the password in MongoDB for existing user, you can use changeUserPassword(). Following is the syntaxdb.changeUserPassword("yourExistingUserName", "yourPassword");Let us first switch the database to admin. Following is the syntax> use adminThis will produce the following outputswitched to db adminNow, display users from the database. Following is the query> db.getUsers();This will produce the following output[    {       "_id" : "admin.John",       "user" : "John",       "db" : "admin",       "roles" : [          {             "role" : "userAdminAnyDatabase",             "db" : "admin"          }       ],       "mechanisms" : [          "SCRAM-SHA-1",          "SCRAM-SHA-256"       ]    } ]Following is the query to change the password for user “John”> db.changeUserPassword("John", "123456");Now the password has been changed with value “123456”.

How to operate on all databases from the MongoDB shell?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

128 Views

To operate on all databases from MongoDB shell, you can use listDatabases along with adminCommand().Let’s say we are using a sample database “test”. At first, check the current database with the help of db command.Following is the query to get the current database> db;This will produce the following outputTestFollowing is the query to operate on all the databases from the Mongo shell> var allDatabaseList = db.adminCommand('listDatabases');Now you need to use printjson() in order to print all databases. Following is the query> printjson (allDatabaseList);This will produce the following output{    "databases" : [       {         ... Read More

How to update value of a key in a list of a json in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

698 Views

Let us first create a collection with documents> db.updateListOfKeyValuesDemo.insertOne( { "StudentDetails":[ { "StudentName":"John", "StudentAge":23, "StudentCountryName":"US" }, { "StudentName":"Carol", "StudentAge":24, "StudentCountryName":"UK" }, { "StudentName":"Bob", "StudentAge":22, "StudentCountryName":"AUS" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b5b759882024390176545") }Following is the query to display all documents from a collection with the help of find() method> db.updateListOfKeyValuesDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9b5b759882024390176545"),    "StudentDetails" : [       {          "StudentName" : "John",          "StudentAge" : 23,          "StudentCountryName" : "US"       },     ... Read More

Is it possible to sum two fields in MongoDB using the Aggregation framework?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

1K+ Views

Yes, it is possible using the $project operator. Let us first create a collection with documents> db.sumTwoFieldsDemo.insertOne({"FirstValue":150, "SecondValue":350}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4bfe15e86fd1496b38cd") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":450, "SecondValue":1550}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4c1215e86fd1496b38ce") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":2560, "SecondValue":2440}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b4c2715e86fd1496b38cf") }Following is the query to display all documents from a collection with the help of find() method> db.sumTwoFieldsDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"),    "FirstValue" : 150,    "SecondValue" : 350 } {    "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"),    "FirstValue" : 450,    "SecondValue" ... Read More

Get Distinct Values with Sorted Data in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

965 Views

Following is the query to get distinct values with sorted data in MongoDBdb.yourCollectionName.distinct("yourFieldName").sort();Let us first create a collection with documents>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e3315e86fd1496b38c3") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e3e15e86fd1496b38c4") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e4415e86fd1496b38c5") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":30, "StudentName":"Chris", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e5115e86fd1496b38c6") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e5715e86fd1496b38c7") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9b1e6515e86fd1496b38c8") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "Stude ... Read More

Advertisements