Found 1661 Articles for Big Data Analytics

How to set limit to $inc in MongoDB?

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

274 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

484 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

428 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

Find the MongoDB document from sub array?

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

431 Views

You can use dot(.) notation to find the document from sub array. Let us first create a collection with documents −> db.findDocumentDemo.insertOne( ...    { ...       "EmployeeDetails" : ...       { ...          "EmployeeAppraisalTime": ... ...          [ ... ...             {"EmployeeDesignation": "Developer", "Salary": 45000}, ...             {"EmployeeDesignation": "Tester", "Salary": 30000}, ...             {"EmployeeDesignation": "HR", "Salary": 22000}, ...             {"EmployeeDesignation": "Accountant", "Salary": 18000} ...          ] ... Read More

Increment a field in MongoDB document which is embedded?

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

203 Views

Let’s say, here we are incrementing StudentScores for MongoDB which is inside StudentDetails −... "StudentScores": { ...    "StudentMathScore": 90, ...    "StudentMongoDBScore": 78 ... }Let us first create a collection with documents −> db.embeddedValueIncrementDemo.insertOne( ...    { ...       "StudentDetails": { ...          "StudentScores": { ...             "StudentMathScore": 90, ...             "StudentMongoDBScore": 78 ...          } ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b670345990cee87fd896") }Following is the query to display ... Read More

How to increment inside the update() function in MongoDB?

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

301 Views

You can use $inc operator to increment. Let us first create a collection with documents −> db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Chris", "PlayerScore":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b3f4345990cee87fd893") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"Robert", "PlayerScore":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b3fc345990cee87fd894") } > db.addInUpdateFunctionDemo.insertOne({"PlayerName":"David", "PlayerScore":99}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2b407345990cee87fd895") }Following is the query to display all documents from a collection with the help of find() method −> db.addInUpdateFunctionDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd2b3f4345990cee87fd893"),    "PlayerName" : "Chris",    "PlayerScore" : 78 } {    "_id" : ObjectId("5cd2b3fc345990cee87fd894"),    "PlayerName" : "Robert", ... Read More

Selecting database inside the JS in MongoDB?

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

128 Views

You can use getSiblingDB() from MongoDB for this using var keyword from JS −anyVariableName= db.getSiblingDB(‘yourDatabaseName’);Let us implement the above syntax in order to select database −> selectedDatabase = db.getSiblingDB('sample');This will produce the following output −SampleNow, insert some documents. Let’s say the collection is ‘selectDatabaseDemo’ −> db.selectDatabaseDemo.insertOne({"ClientName":"John Smith", "ClientAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda794b5667f1cce01d55ad") } > db.selectDatabaseDemo.insertOne({"ClientName":"Carol Taylor", "ClientAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda79565667f1cce01d55ae") }Display all documents from a collection with the help of find() method −db.selectDatabaseDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cda794b5667f1cce01d55ad"),    "ClientName" : "John Smith",   ... Read More

Combine update and query parts to form the upserted document in MongoDB?

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

177 Views

You need to use $set operator along with upsert:true. Let us first create a collection with documents −> db.updateWithUpsertDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a61c345990cee87fd890") } > db.updateWithUpsertDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a624345990cee87fd891") } > db.updateWithUpsertDemo.insertOne({"StudentFirstName":"David", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a62c345990cee87fd892") }Following is the query to display all documents from a collection with the help of find() method −> db.updateWithUpsertDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd2a61c345990cee87fd890"),    "StudentFirstName" : "John",    "StudentAge" : 21 } {    "_id" : ObjectId("5cd2a624345990cee87fd891"),    "StudentFirstName" ... Read More

How to search documents through an integer array in MongoDB?

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

330 Views

You can use $where operator for this. Let us first create a collection with documents −>db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"John", "StudentScores":[45, 78, 89, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a219345990cee87fd88c") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"Larry", "StudentScores":[45, 43, 34, 33]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a22a345990cee87fd88d") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"Chris", "StudentScores":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a23c345990cee87fd88e") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"David", "StudentScores":[99]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a24d345990cee87fd88f") }Following is the query to display all documents from a collection with the help of find() method −> db.searchDocumentArrayIntegerDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd2a219345990cee87fd88c"),    "StudentFirstName" : ... Read More

Advertisements