Filter Documents Based on an Array in MongoDB

AmitDiwan
Updated on 11-May-2020 09:13:33

1K+ Views

To filter documents based on an array, use $elemMatch. The $elemMatch operator matches documents that contain an array field.Let us create a collection with documents −> db.demo453.insertOne( ... { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo453.insertOne( ... { _id: 102, details: [ { Name: "Bob", Marks: 80 }, { Name: "Sam", Marks: 78} ] } ... ) { "acknowledged" : true, "insertedId" : 102 } > db.demo453.insertOne( ... { _id: 103, details: [ { Name: "Carol", Marks: 67 ... Read More

Find Count of Repeated Values in MongoDB Documents

AmitDiwan
Updated on 11-May-2020 09:11:28

194 Views

To get the count of repeated values in different documents, use aggregate(). Let us create a collection with documents −> db.demo452.insertOne({"StudentName":"John", "StudentAge":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3971f552a0ebb0a6f5") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4371f552a0ebb0a6f6") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":25});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4571f552a0ebb0a6f7") }Display all documents from a collection with the help of find() method −> db.demo452.find();This will produce the following output −{ "_id" : ... Read More

Reach Subdata in MongoDB and Display a Particular Document

AmitDiwan
Updated on 11-May-2020 09:02:59

176 Views

In order to reach subdata, you need to use key in MongoDB. Let us create a collection with documents −>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris", "StudentAge":21}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b590e71f552a0ebb0a6e6") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"David", "StudentAge":23}}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b591a71f552a0ebb0a6e7") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike", "StudentAge":22}}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b592271f552a0ebb0a6e8") }Display all documents from a collection with the help of find() method −> db.demo450.find();This will produce the following output −{ "_id" : ObjectId("5e7b590e71f552a0ebb0a6e6"), "Information" : { "StudentDetails" : { "StudentName" : "Chris", "StudentAge" : 21 } } } { "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : { ... Read More

Insert Item to Array Inside an Object in MongoDB

AmitDiwan
Updated on 11-May-2020 09:02:05

904 Views

To insert an item to an already created array inside an object, use MongoDB $push. Let us create a collection with documents −> db.demo449.insertOne( ... { ...    details1: { ...       details2: [{ ...          _id:new ObjectId(), ...             Name:"Chris" ...       }], ...       details3: [{ ...          _id:new ObjectId(), ...          Name:"David" ...       }] ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7a40e971f552a0ebb0a6e3") }Display all documents from ... Read More

Difference Between deleteOne and findOneAndDelete Operations in MongoDB

AmitDiwan
Updated on 11-May-2020 08:59:12

3K+ Views

The findOneAndDelete() deletes single documents from the collection on the basis of a filter and sort criteria as well as it returns the deleted document.The deleteOne() removes single document from the collection.Let us see an example and create a collection with documents −> db.demo448.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a291cbbc41e36cc3caeca") } > db.demo448.insertOne({"Name":"David", "Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a2926bbc41e36cc3caecb") } > db.demo448.insertOne({"Name":"Bob", "Age":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a2930bbc41e36cc3caecc") }Display all documents from a collection with the help of find() method −> db.demo448.find();This will produce the following output −{ "_id" : ObjectId("5e7a291cbbc41e36cc3caeca"), ... Read More

Create Custom Scrollbar with CSS

AmitDiwan
Updated on 08-May-2020 14:29:59

193 Views

To create a custom scrollbar with CSS, the code is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;       height: 200vh; /*to create a scrollbar*/    }    ::-webkit-scrollbar {       width: 20px;    }    p {       font-size: 40px;    }    ::-webkit-scrollbar-track {       box-shadow: inset 0 0 5px grey;       border-radius: 10px;    }    ::-webkit-scrollbar-thumb {       background: rgb(75, 22, 161);       border-radius: 2px;    }    ::-webkit-scrollbar-thumb:hover ... Read More

Create a Browser Window Example with CSS

AmitDiwan
Updated on 08-May-2020 14:24:13

299 Views

To create a browser window example with CSS, the code is as follows −Example Live Demo    body {       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;    }    * {       box-sizing: border-box;    }    .menuBar {       border: 3px solid #f1f1f1;       border-top-left-radius: 4px;       border-top-right-radius: 4px;    }    .menuRow {       padding: 10px;       background: #f1f1f1;       border-top-left-radius: 4px;       border-top-right-radius: 4px;    }    .browserField {       float: left;   ... Read More

Create a Download Link with HTML

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

850 Views

To create a download link with HTML, the code is as follows −Example Live Demo Download Link example Click on the image above to download it OutputThe above code will produce the following output −

Create a Flip Box with CSS

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

810 Views

To create a flip box with CSS, the code is as follows −Example Live Demo    body {       font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;       margin:20px;    }    .flipCard {       background-color: transparent;       width: 300px;       height: 300px;       perspective: 1000px;    }    .innerCard {       position: relative;       width: 100%;       height: 100%;       text-align: center;       transition: transform 0.6s;       transform-style: preserve-3d;       box-shadow: ... Read More

Find Out If an Element is Hidden with JavaScript

AmitDiwan
Updated on 08-May-2020 13:58:35

517 Views

To find out if an element is hidden with JavaScript, the code is as follows −Example Live Demo    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .hiddenDiv {       width: 100%;       padding: 20px;       text-align: center;       background-color: lightblue;       margin-top: 20px;       display: none;       font-size: 20px;    }    .showBtn {       border: none;       padding: 15px;       font-size: 18px;       background-color: rgb(86, 25, ... Read More

Advertisements