Remove Primary Key from MongoDB

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

400 Views

To remove primary key in MongoDB, set _id value to 0 i.e. set the field you want to exclude as 0 in find(). Let us create a collection with documents −> db.demo471.insertOne({"ClientId":101, "ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805711b0f3fa88e2279077") } > db.demo471.insertOne({"ClientId":102, "ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80571db0f3fa88e2279078") } > db.demo471.insertOne({"ClientId":103, "ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805724b0f3fa88e2279079") }Display all documents from a collection with the help of find() method −> db.demo471.find();This will produce the following output −{ "_id" : ObjectId("5e805711b0f3fa88e2279077"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e80571db0f3fa88e2279078"), "ClientId" ... Read More

Return All Ages Records That Are Integers with a MongoDB Query

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

243 Views

To get all the ages that are ints from records having string and int ages records, use $type. The $type in MongoDB $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.demo470.insertOne({"Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805456b0f3fa88e2279070") } > db.demo470.insertOne({"Age":"Unknown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80545cb0f3fa88e2279071") } > db.demo470.insertOne({"Age":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805461b0f3fa88e2279072") } > db.demo470.insertOne({"Age":"Not provided"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80546bb0f3fa88e2279073") }Display all documents from a collection with the help of find() ... Read More

Find Documents Containing a Value in MongoDB Using Regular Expressions

AmitDiwan
Updated on 11-May-2020 09:38:50

77 Views

To find documents containing a particular value with Regular Expressions, use MongoDB $regex. Let us create a collection with documents −> db.demo469.insertOne({"StudentName":"John Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80532fb0f3fa88e227906b") } > db.demo469.insertOne({"StudentName":"Chris Brown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80533ab0f3fa88e227906c") } > db.demo469.insertOne({"StudentName":"John Smith"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805341b0f3fa88e227906d") } > db.demo469.insertOne({"StudentName":"Jace Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805347b0f3fa88e227906e") } > db.demo469.insertOne({"StudentName":"David Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80534eb0f3fa88e227906f") }Display all documents from a collection with the help of find() method −> db.demo469.find();This will produce the following output −{ "_id" ... Read More

Find MongoDB Record That Is Two Levels Deep

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

329 Views

To find a MongoDB record that is two level deep, loop inside MongoDB $where. Let us create a collection with documents −> db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Chris", ...    "StudentAge" : 23 ... }, ... "SecondPosition" : { ...    "StudentName" : "David", ...    "StudentAge" : 20 ... } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e804e2fb0f3fa88e2279069") } > db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Carol", ...    "StudentAge" : 21 ... ... Read More

Delete Item from an Object in MongoDB

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

754 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

674 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

119 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

383 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

688 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

139 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

Advertisements