AmitDiwan has Published 10744 Articles

Update MongoDB variable value with variable itself?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:16:30

509 Views

You cannot update a column value using itself. For this, you can use $set. Let us create a collection with documents −> db.demo256.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a3e91627c0c63e7dba8b") } > db.demo256.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a3ea1627c0c63e7dba8c") } > db.demo256.insertOne({"Name":"David"}); {    "acknowledged" ... Read More

Split a string during MongoDB aggregate

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:15:14

395 Views

For this, use mapReduce(). Let us first create a collection with documents −> db.splitString.insertOne({"StudentName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0849d925ddae1f53b62206") }Following is the query to display all documents from a collection with the help of find() method −> db.splitString.find().pretty();This will produce the following output −{   ... Read More

MongoDB bulk insert for documents

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:15:14

225 Views

For bulk insert, you can use insert() in MongoDB. Let us create a collection with documents −> var manyDocument = db.demo255.initializeUnorderedBulkOp(); > manyDocument.insert( { "Name":"Chris", Age:24} ); > manyDocument.insert( {"Name":"Bob", Age:22 } ); > manyDocument.insert( { "Name":"David", Age:23 } ); > manyDocument.execute(); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" ... Read More

MongoDB query to 'sort' and display a specific number of values

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:13:13

136 Views

To sort in MongoDB, use sort(). For displaying only a specific number of values, use LIMIT. Let us create a collection with documents −> db.demo254.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a0ab1627c0c63e7dba7f") } > db.demo254.insertOne({"Name":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a0b01627c0c63e7dba80") } > db.demo254.insertOne({"Name":"Bob"}); { ... Read More

MongoDB query to change simple field into an object?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:12:51

235 Views

Fir this, you can use $rename. Let us first create a collection with documents −> db.changeSimpleFieldDemo.insertOne({"StudentMarks":58, "StudentSubject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0847a825ddae1f53b62205") }Following is the query to display all documents from a collection with the help of find() method −> db.changeSimpleFieldDemo.find();This will produce the following output ... Read More

Display databases in MongoDB

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:11:23

698 Views

To display number of databases in MongoDB, you need to create atleast one document in a database.Let’s say, you have created a database, but did not added any document in it. Then in the list of databases that particular database won’t be visible.Following is the query to create a database ... Read More

MongoDB query to match and remove element from an array?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:04:39

455 Views

To match and remove element(s) , use MongoDB $pullAll. Let us first create a collection with documents −> db.removeElementsDemo.insertOne({"ListOfNames":["Mike", "Sam", "David", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e071e5a25ddae1f53b62203") }Following is the query to display all documents from a collection with the help of find() method −> db.removeElementsDemo.find().pretty();This ... Read More

How to find by id in MongoDB?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:02:26

12K+ Views

To find by id in MongoDB, use the find() method as in the below syntax −db.findByIdDemo.find({"_id" :yourObjectId});To understand the above syntax, let us create a collection with documents −> db.findByIdDemo.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e07158925ddae1f53b621fc") } > db.findByIdDemo.insertOne({"Value":500}); {    "acknowledged" : true,    "insertedId" : ... Read More

MongoDB query to fetch array values

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 08:00:16

516 Views

Use find() along with $elemMatch to fetch array values. Let us first create a collection with documents −> db.fetchingArrayValuesDemo.insertOne( ... { ...    "StudentName": "David", ...    "StudentDetails": [ ...       { ...          "FatherName": "Bob", ...          "CountryName": "US", ... ... ... Read More

MongoDB indexes not working when executing $elemMatch?

AmitDiwan

AmitDiwan

Updated on 31-Mar-2020 07:54:47

518 Views

To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1}, { sparse : true, background : true } ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 ... Read More

Advertisements