Get Number of Records in MongoDB

AmitDiwan
Updated on 14-May-2020 09:26:07

340 Views

To get number of records, use count() in MongoDB. Let us create a collection with documents −> db.demo697.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d1551299a9f98c9395") } > db.demo697.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7d8551299a9f98c9396") } > db.demo697.insertOne({Name:"David", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d7dd551299a9f98c9397") }Display all documents from a collection with the help of find() method −> db.demo697.find();This will produce the following output −{ "_id" : ObjectId("5ea6d7d1551299a9f98c9395"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea6d7d8551299a9f98c9396"), "Name" : "Bob", "Age" : 23 } { "_id" : ObjectId("5ea6d7dd551299a9f98c9397"), "Name" ... Read More

Pull an Element in Sub-of-Sub Array in MongoDB

AmitDiwan
Updated on 14-May-2020 09:25:43

431 Views

To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −> db.demo679.insertOne( ...    { ...       id:1, ...       "details": [ ...          { ...             CountryName:"US", ...             "information": [ ... ...                { "Name": "Chris", "FirstName": "Name=Chris" }, ... ...                {"Name": "Bob", "FirstName": "Name=Bob" } ...             ] ...          }, ... ... Read More

Build Escape Regular Expressions in MongoDB

AmitDiwan
Updated on 14-May-2020 09:25:04

347 Views

For this, use find() along with //i. Let us create a collection with documents −> db.demo696.insertOne({Message:"/Good/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d664551299a9f98c9391") } > db.demo696.insertOne({Message:"(good)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d67a551299a9f98c9392") } > db.demo696.insertOne({Message:"/Bye/"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d68b551299a9f98c9393") } > db.demo696.insertOne({Message:"(GOOD)"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d693551299a9f98c9394") }Display all documents from a collection with the help of find() method −> db.demo696.find();This will produce the following output −{ "_id" : ObjectId("5ea6d664551299a9f98c9391"), "Message" : "/Good/" } { "_id" : ObjectId("5ea6d67a551299a9f98c9392"), "Message" : "(good)" } { "_id" : ObjectId("5ea6d68b551299a9f98c9393"), ... Read More

Index Collection Using Compound Multikey Index

AmitDiwan
Updated on 14-May-2020 09:24:10

106 Views

For this, use ensureIndex(). Let us create a collection with documents −> db.demo678.ensureIndex({id:1,"details.userId":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo678.insertOne( ...    { ...       id:101, ... ...       "details" : [ ...          { ...             "userId" : "1001", ...             "userName":"Chris" ...          }, ...          { ...             "userId" : "1002", ...             "userName":"David" ...          } ...       ], ...       "otherDetails" : [ ...          { ...             CountryName:"US", ...             EmailId:["Chris@gmail.com","David@gmail.com"] ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4276904263e90dac943fc") }Display all documents from a collection with the help of find() method −> db.demo678.find();This will produce the following output −{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [    { "userId" : "1001", "userName" : "Chris" },    { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [    { "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] } ] }

Work with Array Fields in MongoDB to Match All

AmitDiwan
Updated on 14-May-2020 09:22:26

194 Views

To match all in MongoDB, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo695.insertOne({"ListOfValues":[100, 200, 500, 800]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4c4551299a9f98c938f") } > db.demo695.insertOne({"ListOfValues":[1000, 200, 4000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4cf551299a9f98c9390") }Display all documents from a collection with the help of find() method −> db.demo695.find();This will produce the following output −{ "_id" : ObjectId("5ea6d4c4551299a9f98c938f"), "ListOfValues" : [ 100, 200, 500, 800 ] } { "_id" : ObjectId("5ea6d4cf551299a9f98c9390"), "ListOfValues" ... Read More

Find Documents in MongoDB Based on Matched Objects in Array

AmitDiwan
Updated on 14-May-2020 09:20:21

151 Views

Let us see an example and create a collection with documents −> db.demo694.insertOne( ...    { ...       "details" : ...       [ ...          { ...             "Name" : "Chris", ...             Age:21 ...          }, ...          { ...             "Name" : "David", ...             Age:22 ...          } ...       ] ...    } ... ); {    "acknowledged" ... Read More

Ignore First 4 Values in MongoDB Documents and Display Next 3

AmitDiwan
Updated on 14-May-2020 09:16:27

132 Views

For this, use $slice and set thecount of values to be ignored and displayed. Let us create a collection with documents −> db.demo693.insertOne({Values:[10, 746, 736, 283, 7363, 424, 3535]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea58a04ece4e5779399c07b") } > db.demo693.insertOne({Values:[100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea58a1eece4e5779399c07c") }Display all documents from a collection with the help of find() method −> db.demo693.find();This will produce the following output −{ "_id" : ObjectId("5ea58a04ece4e5779399c07b"), "Values" : [ 10, 746, 736, 283, 7363, 424, 3535 ] } { "_id" : ObjectId("5ea58a1eece4e5779399c07c"), "Values" : ... Read More

MongoDB Query to Find Documents with Specific Firstname and Lastname

AmitDiwan
Updated on 14-May-2020 09:14:20

985 Views

To find documents with specific FirstName and LastName, use $and along with $in. Implement this in MongoDB find(). Let us create a collection with documents −> db.demo692.insertOne({FirstName:"Chris", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585dca7e81adc6a0b396a") } > db.demo692.insertOne({FirstName:"John", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585e2a7e81adc6a0b396b") } > db.demo692.insertOne({FirstName:"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585e7a7e81adc6a0b396c") } > db.demo692.insertOne({FirstName:"John", "LastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585efa7e81adc6a0b396d") } > db.demo692.insertOne({FirstName:"Adam", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585faa7e81adc6a0b396e") }Display all documents from a collection with the help of find() ... Read More

Use of novalidate Attribute in HTML

Arushi
Updated on 14-May-2020 09:12:47

6K+ Views

The novalidate attribute in HTML is used to signify that the form won’t get validated on submit. It is a Boolean attribute and useful if you want the user to save the progress of form filing. If the form validation is disabled, the user can easily save the form and continue & submit the form later. While continuing, the user does not have to first validate all the entries.ExampleYou can try to run the following code to learn how to use novalidate attribute in HTML. In the following example, if you will add text in the  field, then it won’t show ... Read More

Rename a Collection in MongoDB

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

171 Views

To rename a collection in MongoDB, use renameCollection(). Let us create a collection with documents −> db.demo690.insertOne({_id:101, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo690.insertOne({_id:102, Name:"Mike"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo690.insertOne({_id:103, Name:"John"}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : 101, "Name" : "Sam" } { "_id" : 102, "Name" : "Mike" } { "_id" : 103, "Name" : "John" }Following is the query to rename collection −> db.demo690.renameCollection("demo691"); { "ok" ... Read More

Advertisements