Make Nested Queries in MongoDB 4 to Fetch a Specific Document

AmitDiwan
Updated on 13-May-2020 05:27:50

493 Views

For nested queries, let us first create a collection with documents −> db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...          { "ProductName" : "Product-1" }, ...          {"ProductName" : "Product-2"}, ...          { "ProductName" : "Product-3"} ... ...       ] ... ...    } ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e849db8b0f3fa88e22790c2") } > > > > db.demo492.insertOne({ ...    "ProductDetails" : ...    { ...       "StockDetails" : [ ...         ... Read More

Match Date with MongoDB Match

AmitDiwan
Updated on 13-May-2020 05:25:24

4K+ Views

To match date, use $match along with aggregate(). Let us create a collection with documents −> db.demo491.insertOne({"ShippingDate":new ISODate("2020-01-10")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a09b0f3fa88e22790be") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-02-21")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a0eb0f3fa88e22790bf") } > db.demo491.insertOne({"ShippingDate":new ISODate("2020-03-23")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a1db0f3fa88e22790c0") } > db.demo491.insertOne({"ShippingDate":new ISODate()});{    "acknowledged" : true,    "insertedId" : ObjectId("5e849a28b0f3fa88e22790c1") }Display all documents from a collection with the help of find() method −> db.demo491.find();This will produce the following output −{ "_id" : ObjectId("5e849a09b0f3fa88e22790be"), "ShippingDate" : ISODate("2020-01- 10T00:00:00Z") } { "_id" : ObjectId("5e849a0eb0f3fa88e22790bf"), "ShippingDate" : ISODate("2020-02- 21T00:00:00Z") } { ... Read More

Check if All Elements of a Field Are Contained in a Superset in MongoDB

AmitDiwan
Updated on 12-May-2020 14:59:26

163 Views

For all elements of a field in MongoDB, use find() and in that, use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo624.insertOne({"ListOfName":["John", "Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab3ff6c954c74be91e6a5") } > db.demo624.insertOne({"ListOfName":["John", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4026c954c74be91e6a6") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab4076c954c74be91e6a7") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab40e6c954c74be91e6a8") } > ... Read More

Search Array of Objects in MongoDB

AmitDiwan
Updated on 12-May-2020 14:58:03

512 Views

Yes, to search an array of objects, use $unwind in MongoDB aggregate(). To match, use $match. Let us create a collection with documents −> db.demo623.insertOne( ...    { ...       _id:1, ...       details:[ ...          { ...             Name:"Chris" ...          }, ...          { ...             DueDate:new ISODate("2020-01-10") ...          }, ...          { ...             CountryName:"US" ...          } ... ... Read More

Fix Failed to Convert from Type String to Type Date in MongoDB

AmitDiwan
Updated on 12-May-2020 14:57:13

511 Views

To fix this, use $dateFromString in MongoDB aggregate(). The $dateFromString converts a date/time string to a date object.Let us create a collection with documents −> db.demo619.insertOne({"DueDate":"10-10-2020"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7846c954c74be91e69e") } > db.demo619.insertOne({"DueDate":"12-01-2019"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7996c954c74be91e69f") } > db.demo619.insertOne({"DueDate":"28-10-2010"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7ab6c954c74be91e6a0") }Display all documents from a collection with the help of find() method −> db.demo619.find();This will produce the following output −{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : "10-10-2020" } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : "12-01-2019" } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : ... Read More

Remove Document Matching Value in MongoDB Collection

AmitDiwan
Updated on 12-May-2020 14:52:51

297 Views

Remove document using remove(), whose value is matched with $eq from a MongoDB collection. The $eq operator matches documents where the value of a field equals the specified value.Let us create a collection with documents −> db.demo626.insertOne({id:1, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6376c954c74be91e6ae") } > db.demo626.insertOne({id:2, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac63e6c954c74be91e6af") } > db.demo626.insertOne({id:3, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6436c954c74be91e6b0") } > db.demo626.insertOne({id:4, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ac6486c954c74be91e6b1") }Display all documents from a collection with the help of find() method −> db.demo626.find();This ... Read More

Redirect to Another Webpage with JavaScript

AmitDiwan
Updated on 12-May-2020 13:56:13

2K+ Views

To redirect to another webpage using JavaScript, the code is as follows −Example Live Demo Redirect to a Webpage Example Redirect Click the above button to Redirect to another Webpage    document .querySelector(".redirectBtn") .addEventListener("click", redirectFunction);    function redirectFunction() {       location.href("https://tutorialspoint.com/");    } OutputThe above code will produce the following output −On clicking the “Redirect” button we will be redirected to a new site −

Create a Speed Converter with HTML and JavaScript

AmitDiwan
Updated on 12-May-2020 13:49:56

336 Views

To create a speed converter with HTML and JavaScript, the code is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    input, span {       font-size: 20px;    } Speed Converter Type speed in Kilometer per hour to convert it into meter per hour Kilometer Per Hour Meters Per second:    function KmphtoMphConverter(speed) {       document.querySelector(".metersPerSecond").innerHTML = speed * 0.277778;    } OutputThe above code will produce the following output −On entering some speed in kph −

Create Length Converter with HTML and JavaScript

AmitDiwan
Updated on 12-May-2020 13:47:55

1K+ Views

To create a length converter with HTML and JavaScript, the code is as follows −Example Live Demo    body{       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    input,span{       font-size: 20px;    } Length Converter Type length in Kilometers to convert it into meters Kilometers Meters:    function KmtoMConverter(length) {       document.querySelector(".meters").innerHTML=length*1000;    } OutputThe above code will produce the following output −On entering length in kilometers −

Create a Temperature Converter with HTML and JavaScript

AmitDiwan
Updated on 12-May-2020 13:43:48

766 Views

To create a temperature converter with HTML and JavaScript, the code is as follows −Example Live Demo    body{       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    input,span{       font-size: 20px;    } Temperature Converter Type Temperature in celcius to convert it into fahrenheit Celcius Fahrenheit:    function CtoFConverter(temp) {       document.querySelector(".fahrenheit").innerHTML=(temp-32)/1.8;    } OutputThe above code will produce the following output −On entering temperature in celcius −

Advertisements