Found 1661 Articles for Big Data Analytics

How to unset a variable in MongoDB shell?

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

604 Views

Use delete operator to unset variable in MongoDB shell. Following is the syntax −delete yourVariableName;Let us now implement the above syntax in order to unset variable in MongoDB shell. First, print the variable name −> customerDetail;This will produce the following output −2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined : @(shell):1:1Now you can set a value in the above variable. Following is the query −> customerDetail={"CustomerFirstName":"Chris"};This will produce the following output −{ "CustomerFirstName" : "Chris" }Following is the query to show the value of a variable −> customerDetail; This will produce the following output −{ "CustomerFirstName" : "Chris" }Following ... Read More

MongoDB Query to combine AND & OR?

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

2K+ Views

To combine AND & OR in MongoDB, let us first create a collection with documents −>db.combinedAndOrDemo.insertOne({"StudentFirstName":"John", "StudentAge":23, "StudentSkill":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd306dcb64f4b851c3a13e2") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":21, "StudentSkill":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd306f3b64f4b851c3a13e3") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Sam", "StudentAge":24, "StudentSkill":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30701b64f4b851c3a13e4") }Following is the query to display all documents from a collection with the help of find() method −> db.combinedAndOrDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd306dcb64f4b851c3a13e2"),    "StudentFirstName" : "John",    "StudentAge" : 23,    "StudentSkill" : "MongoDB" } {    "_id" : ... Read More

MongoDB query to count the size of an array distinctly?

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

267 Views

Use DISTINCT for distinct elements and then length to get the size of array −db.yourCollectionName.distinct('yourFieldName').length;Let us first create a collection with documents −> db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304f5b64f4b851c3a13dc") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304fab64f4b851c3a13dd") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304fcb64f4b851c3a13de") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30500b64f4b851c3a13df") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30505b64f4b851c3a13e0") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3050ab64f4b851c3a13e1") }Following is the query to display all ... Read More

How to rename a username in MongoDB?

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

2K+ Views

To rename a user, you need to use update() and $set to set the new username. Following is the syntax −db.system.users.update({"user":"yourOldUserName"}, {$set:{"user":"yourNewUserName"}});Firstly, display all the users from the MongoDB database −> use admin; switched to db admin > db.getUsers();This will produce the following output −[    {       "_id" : "admin.Chris",       "user" : "Chris",       "db" : "admin",       "roles" : [          {             "role" : "readWrite",             "db" : "test"          } ... Read More

Performing distinct on multiple fields in MongoDB?

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

2K+ Views

You can use $group operator along with aggregate framework to perform distinct on multiple fields. Let us first create a collection with documents −> db.distinctOnMultipleFieldsDemo.insertOne( ...    { ...       "StudentFirstName" : "Chris", ...       "StudentAge" : 21, ...       "StudentCountryName": "US" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2e518b64f4b851c3a13d7") } > db.distinctOnMultipleFieldsDemo.insertOne( ...    { ...       "StudentFirstName" : "Robert", ...       "StudentAge" : 21, ...       "StudentCountryName": "AUS" ...    } ... ); {    "acknowledged" : true,    "insertedId" ... Read More

How do I search according to fields in inner classes using MongoDB db.coll.find()?

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

165 Views

Use dot notation(.) to search in inner classes using MongoDB. Let us first create a collection with documents −> db.searchInInnerDemo.insertOne( ...    { ...       "StudentFirstName" : "Robert", ...       "StudentTechnicalDetails": ...       { ...          "StudentBackEndTechnology" : "MongoDB", ...          "StudentLanguage" : "Java" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2dd89b64f4b851c3a13d2") } > > db.searchInInnerDemo.insertOne( ...    { ...       "StudentFirstName" : "David", ...       "StudentTechnicalDetails": ...       { ... ... Read More

Get at least one match in list querying with MongoDB?

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

439 Views

Use $in operator to get at least one match. Let us first create a collection with documents −> db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2db5db64f4b851c3a13ce") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Java", "C", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2db71b64f4b851c3a13cf") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Python", "C++", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2db87b64f4b851c3a13d0") } >db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Ruby", "Javascript", "C#", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2dba9b64f4b851c3a13d1") }Following is the query to display all documents from a collection with the help of find() method −> db.atleastOneMatchDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

How can I get a list of databases and collections on a MongoDB server?

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

199 Views

To get a list of all databases, you need to use the below syntax −use admin db.runCommand({listDatabases: 1});To get a list of all collection names of a particular database, you need to use below syntax −use yourDatabaseName; db.getCollectionNames();Let us implement the above syntaxes −Case 1 − To get a list of databases> use admin switched to db admin > db.runCommand({listDatabases: 1});This will produce the following output −{    "databases" : [       {          "name" : "admin",          "sizeOnDisk" : 1675264,          "empty" : false   ... Read More

How to filter array elements in MongoDB?

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

327 Views

You can use $setIntersection operator along with aggregate framework to filter array elements in MongoDB. Let us first create a collection with documents −> db.filterArrayElementsDemo.insertOne( { "Scores": [10, 45, 67, 78, 90, 98, 99, 92] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2d582b64f4b851c3a13c8") }Following is the query to display all documents from a collection with the help of find() method −> db.filterArrayElementsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd2d582b64f4b851c3a13c8"),    "Scores" : [       10,       45,       67,       78,       90,   ... Read More

What is the MongoDB Capped Collection maximum allowable size?

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

249 Views

It is up to you how much space you want. You need to use the parameter size to set. Use below syntax −db.createCollection(‘yourCollectionName’, capped=true, size=yourValue);Let us implement the above syntax in order to allow size for a capped collection −> db.createCollection('cappedCollectionMaximumSize', capped=true, size=1948475757574646); { "ok" : 1 }Let us check the description of the above collection −> db.cappedCollectionMaximumSize.find().explain();This will produce the following output −{    "queryPlanner" : {       "plannerVersion" : 1,       "namespace" : "test.cappedCollectionMaximumSize",       "indexFilterSet" : false,       "parsedQuery" : {       },       "winningPlan" ... Read More

Advertisements