Fetch Records from Subdocument Array in MongoDB

AmitDiwan
Updated on 14-May-2020 08:07:30

116 Views

To fetch records from a subdocument array, use $unwind along with $push. For ids beginning from 234, use regex in MongoDB.Let us create a collection with documents −> db.demo556.insertOne( ... { ...    _id:101, ...    details:[ ...       { ...          id:"234336", ...          Name:"Chris" ...       }, ...       { ...          id:"123456", ...          Name:"Bob" ...       }, ...       { ...          id:"234987", ...          Name:"Carol" ...   ... Read More

MongoDB Query Aggregation Framework to Match a Specific Field Value

AmitDiwan
Updated on 14-May-2020 08:04:09

322 Views

To match a specific field value, use a $match in MongoDB aggregation. Let us create a collection with documents −> db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21bf54b4472ed3e8e85f") } > db.demo555.insertOne({"CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c254b4472ed3e8e860") } > db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c354b4472ed3e8e861") } > db.demo555.insertOne({"CountryName":"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c554b4472ed3e8e862") } > db.demo555.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f21c754b4472ed3e8e863") }Display all documents from a collection with the help of find() method −> db.demo555.find();This will produce the following output: −{ "_id" : ObjectId("5e8f21bf54b4472ed3e8e85f"), "CountryName" : "US" } { "_id" : ObjectId("5e8f21c254b4472ed3e8e860"), "CountryName" ... Read More

Query MongoDB with Like Implementation on Name and Email Fields

AmitDiwan
Updated on 14-May-2020 08:02:33

924 Views

For “like” implementation in MongoDB, use / / and set that specific letter in between. For example −/J/Let us create a collection with documents −> db.demo554.insertOne({"UserName":"John", "UserMailId":"John@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679") } > db.demo554.insertOne({"UserName":"Chris", "UserMailId":"Chris@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a") } > db.demo554.insertOne({"UserName":"Jace", "UserMailId":"Jace@gmail.com"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8f1d1cd1d72c4545cb867b") }Display all documents from a collection with the help of find() method −> db.demo554.find();This will produce the following output −{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" } { "_id" : ObjectId("5e8f1d0cd1d72c4545cb867a"), "UserName" : "Chris", "UserMailId" : "Chris@gmail.com" } { "_id" : ... Read More

MongoDB Syntax for Updating an Object Inside an Array

AmitDiwan
Updated on 14-May-2020 07:59:43

3K+ Views

For this, use findOneAndUpdate() in MongoDB. The findOneAndUpdate() method updates a single document based on the filter and sort criteria.Let us create a collection with documents −> db.demo553.insertOne( ... { ...    id:101, ...    "Name":"John", ...    midExamDetails: ...    [ ...       {"SubjectName":"MySQL", "Marks":70}, ...       {"SubjectName":"MongoDB", "Marks":35} ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8e3da19e5f92834d7f05ed") }Display all documents from a collection with the help of find() method −> db.demo553.find();This will produce the following output −{ "_id" : ObjectId("5e8e3da19e5f92834d7f05ed"), "id" : 101, "Name" : "John", "midExamDetails" ... Read More

Select Documents with Values Above the Average in MongoDB

AmitDiwan
Updated on 14-May-2020 07:56:34

468 Views

Use aggregate() in MongoDB to select documents with values above the average. To find the average, use $avg in MongoDB.Let us create a collection with documents −> db.demo552.insertOne({values:10});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1c9e5f92834d7f05ea") } > db.demo552.insertOne({values:50});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1f9e5f92834d7f05eb") } > db.demo552.insertOne({values:40});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e3b289e5f92834d7f05ec") }Display all documents from a collection with the help of find() method −> db.demo552.find();This will produce the following output −{ "_id" : ObjectId("5e8e3b1c9e5f92834d7f05ea"), "values" : 10 } { "_id" : ObjectId("5e8e3b1f9e5f92834d7f05eb"), "values" : 50 } { "_id" : ObjectId("5e8e3b289e5f92834d7f05ec"), "values" : 40 }Following is the ... Read More

MongoDB Multiple OR Conditions on Same Key

AmitDiwan
Updated on 14-May-2020 07:54:25

2K+ Views

For this, simply use $or once. Let us create a collection with documents −> db.demo551.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d39e5f92834d7f05e5") } > db.demo551.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36d89e5f92834d7f05e6") } > db.demo551.insertOne({"Name":"John Doe"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36de9e5f92834d7f05e7") } > db.demo551.insertOne({"Name":"John Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36e59e5f92834d7f05e8") } > db.demo551.insertOne({"Name":"Carol"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e36ec9e5f92834d7f05e9") }Display all documents from a collection with the help of find() method −> db.demo551.find();This will produce the following output −{ "_id" : ObjectId("5e8e36d39e5f92834d7f05e5"), "Name" : "John" } { "_id" : ObjectId("5e8e36d89e5f92834d7f05e6"), "Name" : "Chris Brown" ... Read More

Perform Multiple Updates with Bulk Operations in MongoDB

AmitDiwan
Updated on 14-May-2020 07:52:15

336 Views

For this, use initializeOrderedBulkOp(). It initializes and returns a new Bulk() operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk.Let us create a collection with documents −>db.demo550.insertOne({"Name":"Chris", "details":[{"Marks":49, Result:"fail"}, {"Marks":58, Result:"fail"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e8e35bd9e5f92834d7f05e4") }Display all documents from a collection with the help of find() method −> db.demo550.find();This will produce the following output −{ "_id" : ObjectId("5e8e35bd9e5f92834d7f05e4"), "Name" : "Chris", "details" : [ { "Marks" : 49, "Result" : "fail" }, { "Marks" : 58, "Result" : "fail" } ] }Following is the query ... Read More

Find a Specified Amount of Records in MongoDB

AmitDiwan
Updated on 14-May-2020 07:49:38

1K+ Views

To find a specific amount of records, use LIMIT() in MongoDB. The method accepts one number type argument, which is the number of documents that you want to be displayed.Let us create a collection with documents −> db.demo549.insertOne({"Name":"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e32889e5f92834d7f05df") } > db.demo549.insertOne({"Name":"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e328c9e5f92834d7f05e0") } > db.demo549.insertOne({"Name":"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e328f9e5f92834d7f05e1") } > db.demo549.insertOne({"Name":"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8e32929e5f92834d7f05e2") }Display all documents from a collection with the help of find() method −> db.demo549.find();This will produce the following output −{ "_id" : ObjectId("5e8e32889e5f92834d7f05df"), "Name" : ... Read More

Use a Textarea as a Multi-Line Text Input Field in HTML

Sai Subramanyam
Updated on 14-May-2020 07:39:04

7K+ Views

To add a multi-line text input, use the HTML tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.Here are the attributes of tag −AttributeValueDescriptionautofocusautofocusSpecifies that on page load the text area should automatically get focus.ColsnumberSpecifies the width of the textarea based on the number of visible character widthsDisableddisabledSpecifies the width of the textarea based on the number of visible character widths.formform_idSpecifies one or more forms.maxlengthnumberSpecifies the maximum number of characters in textarea.NametextAssigns a name to the ... Read More

Fetch Month, Day, Year from ISODate in MongoDB

AmitDiwan
Updated on 14-May-2020 07:13:56

981 Views

The mongo shell provides various methods like ISODate() to return the date, either as a string or as a Date object. ISODate() constructor returns a Date object using the ISODate() wrapper.Let us create a collection with documents −> db.demo548.insertOne({"dueDate":new ISODate("2020-04-09 12:12:40")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e30499e5f92834d7f05de") }Display all documents from a collection with the help of find() method −> db.demo548.find();This will produce the following output −{ "_id" : ObjectId("5e8e30499e5f92834d7f05de"), "dueDate" : ISODate("2020-04- 09T12:12:40Z") }Following is the query to display, month, day, week, year, etc. from ISODate −> db.demo548.aggregate( [ { $project: { Year: { $year: "$dueDate" }, ... Read More

Advertisements