Found 1349 Articles for MongoDB

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

103 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

363 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

138 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

253 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

181 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

How to set limit to $inc in MongoDB?

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

206 Views

To set limit to $inc, use the below syntax −db.yourCollectionName.update({yourFieldName : {$lt : yourValue}}, {$inc : {yourFieldName : yourIncrementValue}}, false, true);Let us first create a collection with documents −> db.limitIncrementDemo.insertOne({"StudentId":101, "StudentScore":95}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2ce9eb64f4b851c3a13c3") } > db.limitIncrementDemo.insertOne({"StudentId":102, "StudentScore":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2cea0b64f4b851c3a13c4") } > db.limitIncrementDemo.insertOne({"StudentId":103, "StudentScore":67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2cea1b64f4b851c3a13c5") } > db.limitIncrementDemo.insertOne({"StudentId":104, "StudentScore":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2cea3b64f4b851c3a13c6") } > db.limitIncrementDemo.insertOne({"StudentId":105, "StudentScore":79}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2cea4b64f4b851c3a13c7") }Following is the query to display all documents ... Read More

Retrieve values from nested JSON array in MongoDB?

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

1K+ Views

To retrieve values from nested JSON array, you can use the below syntax −db.yourCollectionName.find({"yourOuterFieldName.yourInnerFieldName.yourNextInnerFieldName…...N": "yourValue"}).pretty();Let us first create a collection with documents −> db.nestedJSONArrayDemo.insertOne({ ...    "ClientDetails" : ...    { ...       "ClientPersonalDetails" : [ ...          { "CountryName" : "US" }, ...          { "CountryName" : "AUS"}, ...          { "ClientName":"Chris" }, ...          { "ClientName":"David" } ...       ] ...    } ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2cbb2b64f4b851c3a13bc") } > db.nestedJSONArrayDemo.insertOne({ ...    "ClientDetails" : ... ... Read More

Find and replace NumberLong type field in MongoDB?

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

362 Views

Use $set operator along with update() for this. Let us first create a collection with documents. Here we have set one field as NumberLong −> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(101)}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2c960b64f4b851c3a13b6") } > db.findAndReplaceDemo.insertOne({"UserId":NumberLong(110)}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2c966b64f4b851c3a13b7") } > db.findAndReplaceDemo.insertOne({"UserId":NumberLong(101)}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2c969b64f4b851c3a13b8") } > db.findAndReplaceDemo.insertOne({"UserId":NumberLong(120)}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2c96cb64f4b851c3a13b9") } > db.findAndReplaceDemo.insertOne({"UserId":NumberLong(130)}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2c96eb64f4b851c3a13ba") }Following is the query to display all documents from a collection with the help of find() method ... Read More

Find objects created in last week in MongoDB?

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

349 Views

You can use Date() along with $gte operator to find objects created last week. Here the curdate is as follows in my system −> new Date(); ISODate("2019-05-14T08:32:42.773Z")Let us first create a collection with documents −> db.findObectInLastWeekDemo.insertOne({"ShippingDate":new ISODate("2019-05-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda7dc4bf3115999ed511e0") } > db.findObectInLastWeekDemo.insertOne({"ShippingDate":new ISODate("2019-05-02")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda7dc4bf3115999ed511e1") } > db.findObectInLastWeekDemo.insertOne({"ShippingDate":new ISODate("2019-05-08")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda7dc4bf3115999ed511e2") } > db.findObectInLastWeekDemo.insertOne({"ShippingDate":new ISODate("2019-05-07")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda7dc4bf3115999ed511e3") } > db.findObectInLastWeekDemo.insertOne({"ShippingDate":new ISODate("2019-05-09")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda7dc4bf3115999ed511e4") } > ... Read More

Advertisements