AmitDiwan

AmitDiwan

8,390 Articles Published

Articles by AmitDiwan

Page 541 of 839

Display MongoDB with document and subdocument example and update

AmitDiwan
AmitDiwan
Updated on 14-May-2020 370 Views

Following is the syntax showing document and subdocument −db.yourCollectionName.insertOne(    {       yourFiledName:yourValue,       yourFieldName : [          {             yourFiledName1,             yourFiledName2,             .             .             .             N          }       ]    } );Let us see an example create a collection with documents −> db.demo706.insertOne( ...    { ...       PortalName: "GameApplication", ...

Read More

MongoDB query to match documents with array values greater than a specific value

AmitDiwan
AmitDiwan
Updated on 14-May-2020 891 Views

You can use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo701.insertOne({"ListOfValues":[100, 200, 300]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8cf551299a9f98c93b0") } > db.demo701.insertOne({"ListOfValues":[500, 700, 1000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8d8551299a9f98c93b1") } > db.demo701.insertOne({"ListOfValues":[300, 350, 450]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e8e1551299a9f98c93b2") }Display all documents from a collection with the help of find() method −> db.demo701.find();This will produce the following output −{ "_id" : ObjectId("5ea6e8cf551299a9f98c93b0"), "ListOfValues" : [ 100, ...

Read More

MongoDB query to display documents with a specific name irrespective of case

AmitDiwan
AmitDiwan
Updated on 14-May-2020 182 Views

For this, use $regex in MongoDB. We will search for document field value with name “David”, irrespective of case. Let us create a collection with documents −> db.demo700.insertOne( { details: [ { Name:"david" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b1551299a9f98c93ac") } > db.demo700.insertOne( { details: [ { Name:"Chris" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6b9551299a9f98c93ad") } > db.demo700.insertOne( { details: [ { Name:"DAVID" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6bf551299a9f98c93ae") } > db.demo700.insertOne( { details: [ { Name:"David" }]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e6c4551299a9f98c93af") }Display all documents ...

Read More

Multiple atomic updates using MongoDB?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 233 Views

For multiple atomic updates, use update() along with $set. Let us create a collection with documents −> db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") } > db.demo699.insertOne({Name:"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") } > db.demo699.insertOne({Name:"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e381551299a9f98c93a9") } > db.demo699.insertOne({Name:"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6e38a551299a9f98c93aa") }Display all documents from a collection with the help of find() method −> db.demo699.find();This will produce the following output −{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" ...

Read More

How do I get email-id from a MongoDB document and display with print()

AmitDiwan
AmitDiwan
Updated on 14-May-2020 483 Views

For this, use forEach() along with print() to display the email-id values. Let us create a collection with documents −> db.demo690.insertOne({"UserName":"John", "UserEmailId":"John@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db31551299a9f98c939c") } > db.demo690.insertOne({"UserName":"Bob", "UserEmailId":"Bob@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db3c551299a9f98c939d") } > db.demo690.insertOne({"UserName":"David", "UserEmailId":"David@gmail.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6db47551299a9f98c939e") }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "John@gmail.com" } { "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "Bob@gmail.com" } { "_id" ...

Read More

Increment only a single value in MongoDB document?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 172 Views

To update only a single value and increment it in MongoDB, use $inc along with update(). Let us create a collection with documents −> db.demo698.insertOne({Score:78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a4551299a9f98c9398") } > db.demo698.insertOne({Score:56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8a7551299a9f98c9399") } > db.demo698.insertOne({Score:65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8aa551299a9f98c939a") } > db.demo698.insertOne({Score:88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d8b0551299a9f98c939b") }Display all documents from a collection with the help of find() method −> db.demo698.find();This will produce the following output −{ "_id" : ObjectId("5ea6d8a4551299a9f98c9398"), "Score" : 78 } { "_id" : ...

Read More

Pull an element in sub of sub-array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 459 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) regexp in MongoDB?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 384 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

How to index my collection to use a compound multikey index?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 124 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" ] } ] }

Read More

How can I find documents in MongoDB based on the number of matched objects within an array?

AmitDiwan
AmitDiwan
Updated on 14-May-2020 177 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
Showing 5401–5410 of 8,390 articles
« Prev 1 539 540 541 542 543 839 Next »
Advertisements