Aggregate by Country, State, and City in MongoDB Collection

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

970 Views

Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.To aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo620.insertOne({"Country":"IND", "City":"Delhi", state:"Delhi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8de96c954c74be91e6a1") } > db.demo620.insertOne({"Country":"IND", "City":"Bangalore", state:"Karnataka"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e336c954c74be91e6a3") } > db.demo620.insertOne({"Country":"IND", "City":"Mumbai", state:"Maharashtra"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e636c954c74be91e6a4") }Display all documents from a collection with the help of find() method −> db.demo620.find();This will produce the following output −{ "_id" : ObjectId("5e9a8de96c954c74be91e6a1"), ... Read More

Using findOne with Long Type ID in MongoDB

AmitDiwan
Updated on 12-May-2020 06:47:03

177 Views

Yes, we can do that using the NumberLong() datatype in MongoDB. Let us create a collection with documents −> db.demo618.insertOne({_id:NumberLong("6336366454"), Name:"Chris"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366454") } > db.demo618.insertOne({_id:NumberLong("6336366455"), Name:"David"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366455") } > db.demo618.insertOne({_id:NumberLong("6336366456"), Name:"Bob"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366456") }Display all documents from a collection with the help of find() method −> db.demo618.find();This will produce the following output −{ "_id" : NumberLong("6336366454"), "Name" : "Chris" } { "_id" : NumberLong("6336366455"), "Name" : "David" } { "_id" : NumberLong("6336366456"), "Name" : "Bob" }Following is the query to implement MongoDB findOne() ... Read More

Group Query Upon Nested Object in MongoDB

AmitDiwan
Updated on 12-May-2020 06:45:23

326 Views

For this, use dot notation along with $group in MongoDB. Let us create a collection with documents −> db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "Chris", ...          "Age":32, ...          "Project":"Online Library Management System" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d2b86c954c74be91e69b") } > > db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "David", ...          "Age":34, ...     ... Read More

Aggregate Collection and Group by Field Count in MongoDB

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

682 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

150 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

323 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

300 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

112 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 WeakSet

AmitDiwan
Updated on 12-May-2020 06:14:25

111 Views

The JavaScript WeakSet is used for storing collection of objects. Like set it doesn’t store duplicates.Methods of WeakSet −MethodDescriptionadd(obj)Append new value to the weakSet.delete(obj)Deletes the value from weakSet.has(obj)Returns true or false depending upon if the weakSet object contains the value or not.length()Returns the weakSet object lengthFollowing is the code for the WeakSet in JavaScript −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 18px;       font-weight: 500;       color: red;    } ... Read More

Advertisements