Found 1349 Articles for MongoDB

Find the MongoDB document from sub array?

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

358 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

131 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

230 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

89 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

117 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

243 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

MongoDB query to get record beginning with specific element from an array?

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

149 Views

You can use dot(.) notation along with array index to get record beginning with specific element. Let us first create a collection with documents −>db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Chris", "PlayerScore":[780, 9000, 456, 789, 987]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd29fed345990cee87fd889") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"Robert", "PlayerScore":[890, 670, 890, 54646, 42424]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a00c345990cee87fd88a") } >db.arrayStartsWithElementDemo.insertOne({"PlayerName":"David", "PlayerScore":[909090, 896555, 3344433, 78900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2a025345990cee87fd88b") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayStartsWithElementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd29fed345990cee87fd889"),   ... Read More

Aggregate a $slice to get an element in exact position from a nested array in MongoDB?

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

142 Views

You can use aggregate framework for this. Let us first create a collection with documents −>db.exactPositionDemo.insertOne({"StudentName":"John", "StudentScores":[78, 98, 56, 45, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd29a1c345990cee87fd883") }Following is the query to display all documents from a collection with the help of find() method −> db.exactPositionDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd29a1c345990cee87fd883"),    "StudentName" : "John",    "StudentScores" : [       78,       98,       56,       45,       89    ] }Case 1 − Query to aggregate a $slice to get an ... Read More

What does createdCollectionAutomatically mean in MongoDB?

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

167 Views

If a collection does not exist then MongoDB creates a collection in indexing part. The createdCollectionAutomatically tells that the operation created a collection.For our example, let us create a collection with an index −> db.createCollectionDemo.createIndex({"ClientCountryName":1});This will produce the following output −{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us create a collection with documents −> db.createCollectionDemo.insertOne({"ClientName":"Larry", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd2950be3526dbddbbfb612") }Following is the query to display all documents from a collection with the help of find() method −> db.createCollectionDemo.find().pretty();This will produce the following output ... Read More

MongoDB equivalent of `select distinct(name) from collectionName where age = “25”`?

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

106 Views

You can use distinct() to get the equivalent of select distinct. Let us first create a collection with documents −> db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"John", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12759e3526dbddbbfb60b") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Larry", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12768e3526dbddbbfb60c") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"David", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12773e3526dbddbbfb60d") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Carol", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd1277ee3526dbddbbfb60e") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Sam", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd12793e3526dbddbbfb60f") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Larry", "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd127a3e3526dbddbbfb610") } > db.distinctNameAndAgeDemo.insertOne({"ClientFirstName":"Carol", "Age":26}); ... Read More

Advertisements