Update Salary Field Value by 10% for Each Employee in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:38:00

1K+ Views

Let us first create a collection with documents −> db.demo417.insertOne({"EmployeeName":"Chris", "EmployeeSalary":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ebbb912067e57771ae4") } > db.demo417.insertOne({"EmployeeName":"Mike", "EmployeeSalary":1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723ed7b912067e57771ae5") }Display all documents from a collection with the help of find() method −> db.demo417.find();This will produce the following output −{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 500 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1000 }Following is the query to update salary field value with 10 percent of each employee in employee collection −> db.demo417.update({ "EmployeeName": { $in: ["Chris", "Mike"] } }, ... Read More

Convert Collection to Capped in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:35:55

229 Views

To convert collection to capped, use runCommand(). It provides a helper to run specified database commands. Let us first create a collection with documents −> db.demo416.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a7bb912067e57771adf") } > db.demo416.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a81b912067e57771ae0") } > db.demo416.insertOne({"StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e723a84b912067e57771ae1") }Display all documents from a collection with the help of find() method −> db.demo416.find();This will produce the following output −{ "_id" : ObjectId("5e723a7bb912067e57771adf"), "StudentName" : "David" } { "_id" : ObjectId("5e723a81b912067e57771ae0"), "StudentName" : "Mike" } { "_id" : ObjectId("5e723a84b912067e57771ae1"), "StudentName" : ... Read More

Select Specific Columns in MongoDB Query

AmitDiwan
Updated on 03-Apr-2020 13:28:16

7K+ Views

To select specific columns, you can ignore the rest of them i.e. to hide those columns, set them to 0. Let us first create a collection with documents −> db.demo415.insertOne({"ClientName":"Robert", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e72329db912067e57771adc") } > db.demo415.insertOne({"ClientName":"David", "ClientCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7232acb912067e57771add") } > db.demo415.insertOne({"ClientName":"Bob", "ClientCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7232b4b912067e57771ade") }Display all documents from a collection with the help of find() method −> db.demo415.find();This will produce the following output −{ "_id" : ObjectId("5e72329db912067e57771adc"), "ClientName" : "Robert", "ClientCountryName" : "US" } { "_id" : ObjectId("5e7232acb912067e57771add"), ... Read More

Find by ID on an Array of Objects in MongoDB Database

AmitDiwan
Updated on 03-Apr-2020 13:26:08

842 Views

To find by _id on an array of objects, use aggregation and avoid using find(). Let us first create a collection with documents −> db.demo414.insertOne( ...    { ...       "_id": "110", ...       "details":[ ...          { ...             "StudentName":"John", ...             "StudentMarks":56 ...          }, ...          { ...             "StudentName":"Robert", ...             "StudentMarks":98 ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the help of find() method −> db.demo414.find();This will produce the following output −{ "_id" : "110", "details" : [ { "StudentName" : "John", "StudentMarks" : 56 }, { "StudentName" : "Robert", "StudentMarks" : 98 } ] }Following is the query to find by _id on an array of objects −> db.demo414.aggregate([{$unwind: "$details"}, {$match:{"details.StudentMarks" :56}}] )This will produce the following output −{ "_id" : "110", "details" : { "StudentName" : "John", "StudentMarks" : 56 } }

Exclude Nested Fields in MongoDB with a Wildcard

AmitDiwan
Updated on 03-Apr-2020 13:24:14

914 Views

Achieve this with aggregation pipeline. Let us first create a collection with documents −> db.demo413.insertOne( ...    { ...       "_id": "101", ...       "details": { ...          "Info1": { ...             Name:"Chris", ...             Age:21 ...          }, ...          "Info2": { ...             Name:"David", ...             Age:23 ...          } ...       } ...    } ... ); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() method −> db.demo413.find();This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris", "Age" : 21 }, "Info2" : { "Name" : "David", "Age" : 23 } } }Following is the query to exclude nested fields −> db.demo413.aggregate([ ...    { $project: { "details" : { $objectToArray: "$details" } } }, ...    { $project: { "details.v.Age" : 0} }, ...    { $project: { "details" : { $arrayToObject: "$details"} } } ... ]);This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris" }, "Info2" : { "Name" : "David" } } }

Update an Array of Strings Nested Within an Array of Objects in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:22:52

255 Views

Let us create a collection with documents −> db.demo411.aggregate( ...    [ ...       {$project : { ...          _id : 0, ...          Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ...          } ...       } ...    ] ... ) { "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] } > db.demo412.insertOne( ...    { ...       "Information1" : [ ...          { ...           ... Read More

Get Only Values in Arrays with MongoDB Aggregate

AmitDiwan
Updated on 03-Apr-2020 13:20:04

929 Views

Let us create a collection with documents −> db.demo411.insertOne( ...    { ...       "Information" : [ ...          { ...             "Name1" : "Chris", ...             "Name2" : "David" ...          }, ...          { ...             "Name1" : "John", ...             "Name2" : "John" ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" ... Read More

Remove Elements Not Matching Conditions in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:18:01

405 Views

To remove elements, use $pull and for such conditions, use $ne. The $ne in MongoDB is used to select the documents where the value of the field is not equal to the specified value.Let us create a collection with documents −> db.demo410.insertOne( ...    { ...       details: [{isMarried:false}, {isMarried:true}, {isMarried:false}, {isMarried:"Chris"}] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70efc515dc524f70227681") }Display all documents from a collection with the help of find() method −> db.demo410.find();This will produce the following output −{ "_id" : ObjectId("5e70efc515dc524f70227681"), "details" : [ { "isMarried" : false }, { ... Read More

Find MongoDB Documents with a Specific String

AmitDiwan
Updated on 03-Apr-2020 13:15:44

211 Views

To find documents with a specific string, use find() and in that search for a string with regex. Let us create a collection with documents −> db.demo409.insertOne({"Name":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4e515dc524f7022767c") } > db.demo409.insertOne({"Name":"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4ec15dc524f7022767d") } > db.demo409.insertOne({"Name":"Robert Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4f415dc524f7022767e") } > db.demo409.insertOne({"Name":"David Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4fe15dc524f7022767f") }Display all documents from a collection with the help of find() method −> db.demo409.find();This will produce the following output −{ "_id" : ObjectId("5e70e4e515dc524f7022767c"), "Name" ... Read More

Understanding MongoDB Query Plan

AmitDiwan
Updated on 03-Apr-2020 13:13:53

545 Views

To get a query plan in MongoDB, use explain(). The $explain operator gives information on the query plan.Let us create a collection with documents −> db.demo408.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3a115dc524f70227678") } > db.demo408.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3a715dc524f70227679") } > db.demo408.insertOne({"Value":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3ac15dc524f7022767a") } > db.demo408.insertOne({"Value":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3af15dc524f7022767b") } > db.demo408.createIndex({Value:1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Display all documents from a collection with the help ... Read More

Advertisements