Found 1661 Articles for Big Data Analytics

Can I query on a MongoDB index if my query contains the $or operator?

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

206 Views

Yes, you can do that. First, you need to create an index and then use explain(). Let us first create a MongoDB index. Following is the query:> db.indexOrQueryDemo.ensureIndex({"First":1});This will produce the following output{    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 2,    "numIndexesAfter" : 3,    "ok" : 1 }The query to create second index is as follows> db.indexOrQueryDemo.ensureIndex({"Second":1});This will produce the following output{    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 3,    "numIndexesAfter" : 4,    "ok" : 1 }Following is the query for $or operator with indexes. We have used explain() as well here> db.indexOrQueryDemo.find({$or:[{First:1}, {Second:2}]}).explain();This will produce ... Read More

How to retrieve the documents whose values end with a particular character in MongoDB?

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

2K+ Views

Following is the syntax to retrieve the documents whose values end with a particular character in MongoDBdb.yourCollectionName.find({yourFieldName: {$regex: "yourEndingCharacter$"}}).pretty();Let us first create a collection with documents>db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Adam", "StudentAge":25, "StudentCountryName":"LAOS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c45b32d66697741252456") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Sam", "StudentAge":24, "StudentCountryName":"ANGOLA"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c45c02d66697741252457") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Robert", "StudentAge":21, "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c45cb2d66697741252458") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Chris", "StudentAge":20, "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c45d92d66697741252459") } >db.retrieveDocumentsWithEndsWithParticularCharacterDemo.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9c45eb2d6669774125245a") }Following is the query to display all documents from a collection ... Read More

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

483 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

1K+ 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

251 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

896 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

159 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

766 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

Advertisements