List Collections in MongoDB

AmitDiwan
Updated on 12-May-2020 08:19:33

205 Views

To list collections, use getCollectionNames() in MongoDB. Following is the syntax −db.getCollectionNames();Let us implement the above syntax in order to list all collection names from the test database −> db.getCollectionNames();This will produce the following output −[    "arrayDemo",    "arrayFieldIsNotEmptyDemo",    "characterInFieldsDemo",    "checkFieldExistDemo",    "compareTwoFields",    "comparingTwoFieldsDemo",    "convertTextToDateTypeDemo",    "countGroupByDemo",    "demo407",    "demo408",    "demo409",    "demo410",    "demo411",    "demo412",    "demo413",    "demo414",    "demo415",    "demo416",    "demo417",    "demo418",    "demo419",    "demo543",    "demo544",    "demo545",    "demo546",    "demo547",    "demo548",    "demo549",    "demo550",    "demo551",    "demo552",    "demo553", ... Read More

Use a Simple Drop Down List in HTML Forms

seetha
Updated on 12-May-2020 08:16:56

12K+ Views

With HTML, you can create a simple drop-down list of items to get user input in HTML forms. A select box also called drop-down box provides an option to list down various options in the form of drop-down list, from where a user can select one or more options. The tag is used to create a drop-down list in HTML, with the tag.Here’s the list of attributes of tag:Sr.NoAttribute & Description1NameUsed to give a name to the control which is sent to the server to be recognized and get the value.2SizeThis can be used to present a ... Read More

Get Specific Elements from Embedded Array in MongoDB

AmitDiwan
Updated on 12-May-2020 08:15:34

410 Views

To get specific elements, use $match with dot notation. Let us create a collection with documents −> db.demo641.insert( ...    { ...       ProductId:101, ...       "ProductInformation": ...      (                            [ ...          { ...             ProductName:"Product-1", ...             "ProductPrice":1000 ...          }, ...          { ...             ProductName:"Product-2", ...             "ProductPrice":500 ... ... Read More

Projection of Arrays to Get the First Element from MongoDB Documents

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

722 Views

If you want the first element from array, you can use $slice along with $gte. Let us create a collection with documents −> db.demo640.insertOne({Name:"John", "Scores":[80, 90, 75]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c2eb86c954c74be91e6e0") } > db.demo640.insertOne({Name:"Chris", "Scores":[85, 70, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c2ece6c954c74be91e6e1") }Display all documents from a collection with the help of find() method −> db.demo640.find();This will produce the following output −{ "_id" : ObjectId("5e9c2eb86c954c74be91e6e0"), "Name" : "John", "Scores" : [ 80, 90, 75 ] } { "_id" : ObjectId("5e9c2ece6c954c74be91e6e1"), "Name" : "Chris", "Scores" : [ 85, 70, 89 ] }Following ... Read More

MongoDB Aggregation Framework with Group Query Example

AmitDiwan
Updated on 12-May-2020 08:10:13

186 Views

For this, use $group in MongoDB aggregation. Let us create a collection with documents −> db.demo639.insertOne( ...    { ...       "_id" : 1, ...       "CountryName" : "US", ...       "Info1" : { ...          "Name" : "Chris", ...          "SubjectName" : "MySQL", ...          "Marks" : 78 ...       }, ...       "Info2" : { ...          "Name" : "Chris", ...          "SubjectName" : "MySQL", ...          "Marks" : 78 ... Read More

Filtering MongoDB Items by Fields and Subfields

AmitDiwan
Updated on 12-May-2020 08:05:22

514 Views

To filter items by fields and subfields, use dot notation. Let us create a collection with documents −> db.demo638.insert({Name:"Chris"}); WriteResult({ "nInserted" : 1 }) > db.demo638.insert({Name:"David", details:{Subject:"MongoDB"}}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo638.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e9c28666c954c74be91e6de"), "Name" : "Chris" }    {       "_id" : ObjectId("5e9c28866c954c74be91e6df"),       "Name" : "David",       "details" : {          "Subject" : "MongoDB"    } }Following is the query to filter items by multiple fields and subfields −> ... Read More

Interpret Query Plan Information for MongoDB Collection Find Method

AmitDiwan
Updated on 12-May-2020 08:02:14

147 Views

For information on the query plan, use explain() in MongoDB. Let us create a collection with documents −> db.demo637.ensureIndex({ClientName:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo637.insert({ClientName:"John"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Bob"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Johnson"}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo637.find();This will produce the following output −{ "_id" : ObjectId("5e9c26916c954c74be91e6db"), "ClientName" : "John" } { "_id" : ObjectId("5e9c26936c954c74be91e6dc"), "ClientName" : "Bob" } { "_id" : ObjectId("5e9c269d6c954c74be91e6dd"), "ClientName" : "Johnson" }Case ... Read More

MongoDB Query to Display Alternative Documents with MapReduce Function

AmitDiwan
Updated on 12-May-2020 07:54:12

164 Views

Let us create a collection with documents −> db.demo636.insert({id:1}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:2}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:3}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:4}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:5}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:6}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo636.find();This will produce the following output −{ "_id" : ObjectId("5e9c127b6c954c74be91e6d2"), "id" : 1 } { "_id" : ObjectId("5e9c127e6c954c74be91e6d3"), "id" : 2 } { "_id" : ObjectId("5e9c127f6c954c74be91e6d4"), "id" : 3 } { "_id" : ObjectId("5e9c12816c954c74be91e6d5"), "id" : 4 } { ... Read More

Calculate Frequency of Duplicate Names Using MongoDB Aggregate

AmitDiwan
Updated on 12-May-2020 07:49:42

227 Views

To calculate frequency, group with $group in aggregate(). Let us create a collection with documents −> db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f06c954c74be91e6cc") } > db.demo635.insertOne({Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f46c954c74be91e6cd") } > db.demo635.insertOne({Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f66c954c74be91e6ce") } > db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f86c954c74be91e6cf") } > db.demo635.insertOne({Name:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10fb6c954c74be91e6d0") } > db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10fc6c954c74be91e6d1") }Display all documents from a collection with the help of find() method −> db.demo635.find();This ... Read More

Update Document with Marks Value in MongoDB for Student David

AmitDiwan
Updated on 12-May-2020 07:45:25

227 Views

Use forEach() and traverse to find student name David update new marks for the same student. Let us create a collection with documents −> db.demo634.insertOne({Name:"Chris", "Marks":76}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0ea66c954c74be91e6c9") } > db.demo634.insertOne({Name:"Bob", "Marks":67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0ead6c954c74be91e6ca") } > db.demo634.insertOne({Name:"David", "Marks":37}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0eb76c954c74be91e6cb") }Display all documents from a collection with the help of find() method −> db.demo634.find();This will produce the following output −{ "_id" : ObjectId("5e9c0ea66c954c74be91e6c9"), "Name" : "Chris", "Marks" : 76 } { "_id" : ObjectId("5e9c0ead6c954c74be91e6ca"), "Name" : "Bob", "Marks" : ... Read More

Advertisements