Aggregate Collection and Group by Field Count in MongoDB

AmitDiwan
Updated on 12-May-2020 06:43:28

698 Views

In MongoDB aggregate(), use $group and aggregate collection. Let us create a collection with documents −> db.demo616.insertOne({"details":{"Name":"Chris", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfac65492f6c60d00283") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":22}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb065492f6c60d00284") } > db.demo616.insertOne({"details":{"Name":"Bob", "Age":23}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb865492f6c60d00285") } > db.demo616.insertOne({"details":{"Name":"Sam", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfbd65492f6c60d00286") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":24}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfc165492f6c60d00287") }Display all documents from a collection with the help of find() method −> db.demo616.find();This will produce the following output −{ "_id" ... Read More

Use Type in MongoDB

AmitDiwan
Updated on 12-May-2020 06:41:09

155 Views

The $type selects documents where the value of the field is an instance of the specified BSON type. Let us create a collection with documents −> db.demo615.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"100"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"300"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":300}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo615.find();This will produce the following output −{ "_id" : ObjectId("5e99bb3465492f6c60d0027f"), "Value" : 100 } { "_id" : ObjectId("5e99bb3865492f6c60d00280"), "Value" : "100" } { "_id" : ObjectId("5e99bb3c65492f6c60d00281"), "Value" : "300" } { "_id" : ObjectId("5e99bb4265492f6c60d00282"), "Value" ... Read More

What is Natural 1 in MongoDB

AmitDiwan
Updated on 12-May-2020 06:40:05

1K+ Views

The ({$natural − 1}) works like LIFO(LAST IN FIRST OUT), that means last inserted document will be shown first.Let us create a collection with documents −> db.demo614.insertOne({"CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cddf6b89257f5584d8e") } > db.demo614.insertOne({"CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce0f6b89257f5584d8f") } > db.demo614.insertOne({"CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce3f6b89257f5584d90") } > db.demo614.insertOne({"CountryName":"IND"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cebf6b89257f5584d91") }Display all documents from a collection with the help of find() method −> db.demo614.find();This will produce the following output −{ "_id" : ObjectId("5e988cddf6b89257f5584d8e"), "CountryName" : "US" } { ... Read More

JavaScript Importing and Exporting Modules

AmitDiwan
Updated on 12-May-2020 06:24:44

333 Views

To import and export modules using JavaScript, the code is as follows −Note − To run this example you will need to run a localhost server.INDEX.htmlExample Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;    } JavaScript Importing and Exporting Modules IMPORT Click on the above button to import module script.jsimport test from './sample.js'; document.querySelector('.Btn').addEventListener('click',()=>{    test(); })sample.jslet resultEle = document.querySelector(".result"); export default function testImport(){    resultEle.innerHTML = 'Module testImport has been imported'; }OutputOn clicking the ‘IMPORT’ button −

Generate Random Hex Codes of Color in JavaScript

AmitDiwan
Updated on 12-May-2020 06:19:40

308 Views

To generate random hex codes of color using JavaScript, the code is as follows −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       box-sizing: border-box;       font-size: 18px;       font-weight: 500;       color: white;       width: 150px;       height: 150px;       text-align: center;       padding-top: 4%;    } Generate random hex codes of color GENERATE Click on the above button to generate a random hex color code    let resultEle = document.querySelector(".result");    let hexCode = "0123456789ABCDEF";    let Color = "#";    document.querySelector(".Btn").addEventListener("click", () => {       for (let i = 0; i < 6; i++)          Color += hexCode[Math.floor(Math.random() * 16)];       resultEle.style.backgroundColor = Color;       resultEle.innerHTML = Color;    }); OutputOn clicking the ‘GENERATE’ button −

JavaScript File Name Property

AmitDiwan
Updated on 12-May-2020 06:16:53

121 Views

The JavaScript File WebAPI file.name property returns only the name of the file without the path.Following is the code for the File WebApi File.name property −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: red;    } JavaScript file.name property Upload a file using the above input type to get its file name    let resultEle = document.querySelector(".result");    document    .querySelector(".fileInput")    .addEventListener("change", (event) => {       resultEle.innerHTML += "File name = " + event.target.files[0].name;    }); OutputOn clicking the ‘Choose file’ button and selecting a file −

JavaScript unescape() Function with Example

AmitDiwan
Updated on 12-May-2020 06:11:52

281 Views

The JavaScript unescape() function is used to decode an encoded string. It is deprecated in JavaScript version 1.5.Following is the code for the unescape() function −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample,    .result {       font-size: 18px;       font-weight: 500;       color: red;    }    .result {       color: blueviolet;    } JavaScript unescape() property CLICK HERE CLICK the above button to decode the above url ... Read More

JavaScript Undefined Property

AmitDiwan
Updated on 12-May-2020 06:09:36

215 Views

The JavaScript undefined property specifies if a variable has been declared or assigned a value yet.Following is the code for the JavaScript undefined property −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 18px;       font-weight: 500;       color: red;    } JavaScript undefined property CLICK HERE CLICK the above button to know if variable age has been defined or not    let sampleEle = document.querySelector(".sample");    let ... Read More

JavaScript Promises

AmitDiwan
Updated on 11-May-2020 14:13:06

1K+ Views

Promises in JavaScript allow us to do asynchronous operations where the value is not known in advanced when the promise was being created. A promise can have three states pending, fulfilled and rejected.Following is the code for promises in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample {       font-size: 18px;       font-weight: 500;       color: red;    } JavaScript Promises CLICK HERE Click on the above button to display username ... Read More

JavaScript Numbers Example

AmitDiwan
Updated on 11-May-2020 13:51:46

178 Views

Following is an example for numbers in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .sample{       font-size: 18px;       font-weight: 500;       color: red;    } JavaScript Numbers CLICK HERE Click on the above button to see numbers in JavaScript    let sampleEle = document.querySelector(".sample");    let a =22;    let b = 1.523;    let c = 99;    document.querySelector(".Btn").addEventListener("click", () => {       sampleEle.innerHTML = 'a = ' + a + '';       sampleEle.innerHTML += 'b =' + b + '';       sampleEle.innerHTML += 'c = ' + c + '';       sampleEle.innerHTML += 'a + c = ' + (a+c) + '';    }); OutputOn clicking the ‘CLICK HERE’ button −

Advertisements