
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
Found 1349 Articles for MongoDB

2K+ Views
To create a collection correctly you need to use a MongoDB object in call i.e.db.createCollection("yourCollectionName");Let us implement the above syntax in order to create a collection and call it using a MongoDB object −> use sample; switched to db sample > db.createCollection("employeeInformation"); { "ok" : 1 }Display all the collections from the above ‘sample’ database −> db.getCollectionNames();This will produce the following output −[ "arraySizeErrorDemo", "atleastOneMatchDemo", "basicInformationDemo", "combinedAndOrDemo", "convertSQLQueryDemo", "copyThisCollectionToSampleDatabaseDemo", "countOrSizeDemo", "distinctOnMultipleFieldsDemo", "documentWithAParticularFieldValueDemo", "employee", "employeeInformation", "findListOfIdsDemo", "findMimimumElementInArrayDemo", "findSubstring", "getAllRecordsFromSourceCollectionDemo", "getElementWithMaxIdDemo", "insertDocumentWithDateDemo", "internalArraySizeDemo", ... Read More

248 Views
Let us first create a collection with documents −> db.embeddedDocumentDemo.insertOne( ... { ... "CustomerDetails":[ ... {"CustomerName":"Chris", "CustomerPurchasePrice":3000}, ... {"CustomerName":"Robert", "CustomerPurchasePrice":4500}, ... {"CustomerName":"David", "CustomerPurchasePrice":1000}, ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd32347edc6604c74817ccd") }Following is the query to display all documents from a collection with the help of find() method −> db.embeddedDocumentDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd32347edc6604c74817ccd"), "CustomerDetails" : [ { "CustomerName" ... Read More

237 Views
Let us first create a collection with documents −> db.projectionAnElementDemo.insertOne( ... { ... "CustomerId":100, ... "CustomerDetails": [ ... { ... "CustomerName": "Chris", ... "CustomerCountryName": "US" ... }, ... { ... "CustomerName": "Robert", ... "CustomerCountryName": "UK" ... } ... ] ... } ... ); { "acknowledged" : true, ... Read More

496 Views
You can use $set operator for this. Let us first create a collection with documents −> db.updateSubObjectDemo.insertOne( ... { ... ... "ClientId" : 100, ... "ClientDetails" : { ... "ClientFirstName" : "Adam" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd31434b64f4b851c3a13e9") }Following is the query to display all documents from a collection with the help of find() method −> db.updateSubObjectDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd31434b64f4b851c3a13e9"), "ClientId" : 100, "ClientDetails" : { ... Read More

151 Views
Let us first create a collection with a document −>db.replacingEntireDocumentDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3119bb64f4b851c3a13e8") }Following is the query to display document from a collection with the help of find() method −> db.replacingEntireDocumentDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCountryName" : "US" }Following is the query to update a MongoDB document while replacing the entire document −>db.replacingEntireDocumentDemo.update({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentCountryName":"US"}, {"StudentFirstName":"David", "StudentLastName":"Miller", "StudentCountryName":"AUS"}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us display all the records from the collection ... Read More

964 Views
Use unique and set it to TRUE. Let us implement the same by creating index and setting two columns to unique −>db.keepTwoColumnsUniqueDemo.createIndex({"StudentFirstName":1, "StudentLastName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Now you can insert documents in the above collection −>db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30fd7b64f4b851c3a13e5") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30fe5b64f4b851c3a13e6") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":24}); 2019-05-08T22:50:42.803+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: sample.keepTwoColumnsUniqueDemo index: StudentFirstName_1_StudentLastName_1 dup key: { : "John", : "Smith" } ... Read More

603 Views
Use delete operator to unset variable in MongoDB shell. Following is the syntax −delete yourVariableName;Let us now implement the above syntax in order to unset variable in MongoDB shell. First, print the variable name −> customerDetail;This will produce the following output −2019-05-08T22:29:17.361+0530 E QUERY [js] ReferenceError: customerDetail is not defined : @(shell):1:1Now you can set a value in the above variable. Following is the query −> customerDetail={"CustomerFirstName":"Chris"};This will produce the following output −{ "CustomerFirstName" : "Chris" }Following is the query to show the value of a variable −> customerDetail; This will produce the following output −{ "CustomerFirstName" : "Chris" }Following ... Read More

2K+ Views
To combine AND & OR in MongoDB, let us first create a collection with documents −>db.combinedAndOrDemo.insertOne({"StudentFirstName":"John", "StudentAge":23, "StudentSkill":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd306dcb64f4b851c3a13e2") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":21, "StudentSkill":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd306f3b64f4b851c3a13e3") } >db.combinedAndOrDemo.insertOne({"StudentFirstName":"Sam", "StudentAge":24, "StudentSkill":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30701b64f4b851c3a13e4") }Following is the query to display all documents from a collection with the help of find() method −> db.combinedAndOrDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd306dcb64f4b851c3a13e2"), "StudentFirstName" : "John", "StudentAge" : 23, "StudentSkill" : "MongoDB" } { "_id" : ... Read More

266 Views
Use DISTINCT for distinct elements and then length to get the size of array −db.yourCollectionName.distinct('yourFieldName').length;Let us first create a collection with documents −> db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304f5b64f4b851c3a13dc") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fab64f4b851c3a13dd") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fcb64f4b851c3a13de") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30500b64f4b851c3a13df") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30505b64f4b851c3a13e0") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3050ab64f4b851c3a13e1") }Following is the query to display all ... Read More

2K+ Views
To rename a user, you need to use update() and $set to set the new username. Following is the syntax −db.system.users.update({"user":"yourOldUserName"}, {$set:{"user":"yourNewUserName"}});Firstly, display all the users from the MongoDB database −> use admin; switched to db admin > db.getUsers();This will produce the following output −[ { "_id" : "admin.Chris", "user" : "Chris", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "test" } ... Read More