
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
AmitDiwan has Published 10744 Articles

AmitDiwan
679 Views
To get fields from multiple sub-documents, use MongoDB aggregate with $unwind. Let us create a collection with documents −> db.demo671.insertOne( ... { ... ... "details" : [ ... { ... "id" : "1" ... }, ... { ... CountryName:"US", ... ... Read More

AmitDiwan
845 Views
To access subdocuments in MongoDB, use find() with dot notation. Let us create a collection with documents −> db.demo670.insertOne({ ... id:101, ... "details": ... { ... Name:"Chris", ... Age:21, ... CountryName:"US", ... SubjectName:"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e31d04263e90dac943de") } > ... Read More

AmitDiwan
163 Views
To get the maximum element from the collection, sort in descending order with limit. Let us create a collection with documents −> > db.demo669.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133c04263e90dac943d9") } > db.demo669.insertOne({"Marks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133f04263e90dac943da") } > db.demo669.insertOne({"Marks":89}); { ... Read More

AmitDiwan
203 Views
Use $all for array match. 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.demo668.createIndex({"ListOfSubject":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" ... Read More

AmitDiwan
394 Views
The ObjectId does not accept the seed string. You need to use _id − StringValue. Let us create a collection with documents −> db.demo667.insertOne({_id:"Chris"}); { "acknowledged" : true, "insertedId" : "Chris" } > db.demo667.insertOne({_id:"David"}); { "acknowledged" : true, "insertedId" : "David" } > db.demo667.insertOne({_id:"Chris"}); 2020-04-23T22:01:23.268+0530 E QUERY [js] WriteError: E11000 ... Read More

AmitDiwan
2K+ Views
To display only the employee names with specific salaries, set the salaries in MongoDB $in and fetch the names. Let us create a collection with documents> db.demo666.insertOne({"EmployeeName":"John", "EmployeeSalary":25000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c04824113ea5458c7d0d") } > db.demo666.insertOne({"EmployeeName":"Chris", "EmployeeSalary":35000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c05524113ea5458c7d0e") ... Read More

AmitDiwan
156 Views
To search for ranges, use sort() with limit(). Let us create a collection with documents −> db.demo665.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1424113ea5458c7d08") } > db.demo665.insertOne({"Value":15}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1b24113ea5458c7d09") } > db.demo665.insertOne({"Value":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1e24113ea5458c7d0a") ... Read More

AmitDiwan
461 Views
It is saved in the special system.js collection. For this, use db.system.js.save(). Following is the syntax −db.system.js.save({ _id: "anyFunctionName", value: function (returnValue) { return ‘yourMessage ' + returnValue; } })Let us implement the above syntax. Following is the query −> db.system.js.save({ ... _id: ... Read More

AmitDiwan
826 Views
To count number of documents, use count() in MongoDB. Let us create a collection with documents −> db.demo664.insertOne({_id:1, ClientName:"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo664.insertOne({_id:2, ClientName:"Bob"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo664.insertOne({_id:3, ClientName:"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > ... Read More

AmitDiwan
427 Views
For bulk insert in MongoDB, use initializeUnorderedBulkOp(). Let us create a collection with documents −> var bulkInsertDoc = db.demo663.initializeUnorderedBulkOp(); > bulkInsertDoc.insert( { Name: "John", CountryName:"US"} ); > bulkInsertDoc.insert( { Name: "Chris", CountryName:"UK"} ); > bulkInsertDoc.insert( { Name: "David", CountryName:"AUS"} ); > bulkInsertDoc.execute(); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" ... Read More