Delete Item from an Object in MongoDB

AmitDiwan
Updated on 11-May-2020 09:35:56

746 Views

To delete an item from an object in MongoDB, use $unset. Let us create a collection with documents −> db.demo467.insertOne( ... { ... _id:101, ... "Information":{"Name":"Chris"} ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo467.insertOne( ... { ... _id:102, ... "Information":{"Name":"David"} ... } ... ); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo467.find();This will produce the following output −{ "_id" : 101, "Information" : { "Name" : "Chris" } } { "_id" : 102, "Information" : { "Name" : "David" } }Following ... Read More

Count Stocks with Similar ProductID using MongoDB Nested Group and Sum

AmitDiwan
Updated on 11-May-2020 09:35:34

654 Views

The $group in MongoDB is used to group input documents by the specified _id expression. Let us create a collection with documents −> db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 1 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e80477cb0f3fa88e2279066") } > > db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 2 ... } ... ); {    "acknowledged" : true, ... Read More

Get All Documents Containing Another Document in an Array with MongoDB

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

108 Views

For this, simply use dot notation with find() in MongoDB. Let us create a collection with documents −> db.demo465.insertOne( ... { ...    id: 101, ...    details: [{ ...       Name: "Chris", ...       Info: { ...          Subject: "MongoDB", ...          Marks:67 ...       } ...    }, { ...          Name: "David", ...          Info: { ...             Subject: "MySQL", ...             Marks:78 ...       } ... ... Read More

Exclude Array Type Field Value in MongoDB

AmitDiwan
Updated on 11-May-2020 09:31:01

372 Views

To exclude array type field value, use delete() in MongoDB. Let us create a collection with documents −> db.demo464.insertOne( ... { ... ...    "id" : "101", ...    "details": [ ...       { ...          Name:"Chris" ...       }, ...       { ...          Name:"David" ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7f8832cb66ccba22cc9dda") }Display all documents from a collection with the help of find() method −> db.demo464.find();This will produce the following output −{ "_id" ... Read More

Retrieve Data from a MongoDB Collection

AmitDiwan
Updated on 11-May-2020 09:30:22

670 Views

To return a single document from a collection, use findOne() in MongoDB. Let us create a collection with documents −> db.demo463.insertOne({"StudentName":"Chris Brown", "StudentAge":21, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ec8cb66ccba22cc9dcf") } > db.demo463.insertOne({"StudentName":"David Miller", "StudentAge":23, "StudentCountryName":"UK"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ed5cb66ccba22cc9dd0") } > db.demo463.insertOne({"StudentName":"John Doe", "StudentAge":22, "StudentCountryName":"AUS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ee1cb66ccba22cc9dd1") } > db.demo463.insertOne({"StudentName":"John Smith", "StudentAge":24, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7eefcb66ccba22cc9dd2") }Display all documents from a collection with the help of find() method −> db.demo463.find();This will produce the following output −{ "_id" : ObjectId("5e7f7ec8cb66ccba22cc9dcf"), "StudentName" : "Chris ... Read More

JavaScript Symbol.toStringTag Symbol

AmitDiwan
Updated on 11-May-2020 09:30:17

125 Views

The JavaScript Symbol.toStringTag symbol is a very well known symbol that is a string valued property used for giving a description of an object during its creationFollowing is the code for Symbol.toStringTag symbol −Example Live Demo Document    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    div {       font-size: 20px;       font-weight: 500;    } JavaScript Symbol.toStringTag Symbol CLICK HERE Click on the above button to get the string description for objects    let fillEle = document.querySelector(".sample"); ... Read More

Update Specific MongoDB Document in Array with Set and Positional Operator

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

298 Views

To update a specific document in array with $set and positional $ operator, use MongoDB updateOne(). The updateOne() updates a single document in a collection based on a query filter.Let us create a collection with documents −> db.demo462.insertOne( ... { ...    "id":1, ...    "DueDateDetails": [ ...       { ...          "Name": "David", ...          "Age":21, ...          "CountryName":["US", "UK"] ...       }, ...       { ... ...          "Name": "Chris", ...          "Age":23, ...       ... Read More

MongoDB Query to Update Tag

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

263 Views

To update tag in MongoDB, use the update command. Let us create a collection with documents −> db.demo713.insertOne( ... { ... tags: ...    [ ...       { ...          id:101, ...          Name:"Tag-1" ...       }, ...       { ...          id:102, ...          Name:"Tag-3" ...       }, ...       { ...          id:103, ...          Name:"Tag-3" ...       } ...    ] ... } ... ); {   ... Read More

Creating a Navigation Menu Using CSS Image Sprite

AmitDiwan
Updated on 11-May-2020 09:25:45

593 Views

Image sprite is used to reduce the number of http requests that makes our site’s load time faster.Following is the code for creating a navigation menu using CSS image sprite −Example Live Demo body {    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    margin: 0px; } span {    width: 200px;    height: 300px;    background-color: black; } nav {    background-color: black;    height: 50px;    padding-top: 15px;    padding-left: 10px; } nav a {    font-size: 20px;    color: white;    text-decoration: none;    margin-right: 10px; } .home {    width: 32px;    height: 32px; ... Read More

Cast to ObjectId Failed for Value in MongoDB

AmitDiwan
Updated on 11-May-2020 09:24:54

649 Views

To cast to ObjectId correctly, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo460.insertOne({"_id":"5ab9cbfa31c2ab715d42129e"}); { "acknowledged" : true, "insertedId" : "5ab9cbfa31c2ab715d42129e" }Display all documents from a collection with the help of find() method −> db.demo460.find();This will produce the following output −{ "_id" : "5ab9cbfa31c2ab715d42129e" }Following is the query to cast to objectId −> db.demo460.aggregate( [ idConvert ]) This will produce the following output −{ "_id" : "5ab9cbfa31c2ab715d42129e", "value" : ObjectId("5ab9cbfa31c2ab715d42129e") }

Advertisements