Found 1359 Articles for MongoDB

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

AmitDiwan
Updated on 14-May-2020 09:29:48

350 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
Updated on 14-May-2020 09:27:54

88 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

Get number of records in MongoDB?

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

271 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

Build (escape) regexp in MongoDB?

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

242 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 do I work with array fields in MongoDB to match all?

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

152 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

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

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

87 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 the next 3?

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

75 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

794 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

MongoDB query to rename a collection?

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

88 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

Writing a MongoDB insert statement for multiple insert at a time

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

163 Views

For multiple insert, use insert() in MongoDB. Let us create a collection with document −> db.demo689.insert([ ...    {ClientName:"Chris", "ClientAge":34, "ClientCountryName":"US"}, ...    {ClientName:"David", "ClientAge":28, "ClientCountryName":"UK"}, ...    {ClientName:"Bob", "ClientAge":39, "ClientCountryName":"AUS"}, ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo689.find();This will produce the following output −{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3967"), "ClientName" : "Chris", "ClientAge" : 34, "ClientCountryName" : ... Read More

Advertisements