- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
492 Views
To remove the _id element, you can use the following syntax −db.yourCollectionName.find({}, {'_id': false}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.removingidElementDemo.insertOne({"UserName":"John", ... "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9153fd4afe5c1d2279d6ad") } > db.removingidElementDemo.insertOne({"UserName":"Carol", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154084afe5c1d2279d6ae") } > db.removingidElementDemo.insertOne({"UserName":"David", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154154afe5c1d2279d6af") } > db.removingidElementDemo.insertOne({"UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154204afe5c1d2279d6b0") } > db.removingidElementDemo.insertOne({"UserName":"Chris", "UserAge":20}); { "acknowledged" : true, ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
382 Views
There is no in-built function to count a number of keys in a document. In order to count a number of keys, you need to write some code.Let us create a collection with a document. The query to create a collection with a document is as follows −> db.numberofKeysInADocumentDemo.insertOne({ "UserName":"John", "UserAge":21, "UserEmailId":"john12@gmail.com", "UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9132584afe5c1d2279d6ac") }Display all documents from a collection with the help of find() method. The query is as follows −> db.numberofKeysInADocumentDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c9132584afe5c1d2279d6ac"), "UserName" : "John", "UserAge" : 21, ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
257 Views
You can use “$regex” operator to implement the equivalent of SQL ‘like’ in MongoDB. To implement it, let us create a collection with a document. The query to create a collection with a document is as follows −> db.sqlLikeDemo.insertOne({"UserName":"John Smith", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e124afe5c1d2279d6a5") } > db.sqlLikeDemo.insertOne({"UserName":"John Doe", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e264afe5c1d2279d6a6") } > db.sqlLikeDemo.insertOne({"UserName":"Chris Williams", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e404afe5c1d2279d6a7") } > db.sqlLikeDemo.insertOne({"UserName":"Robert Taylor", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e4d4afe5c1d2279d6a8") } > db.sqlLikeDemo.insertOne({"UserName":"John Brown", "UserAge":27}); { "acknowledged" ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
556 Views
PyMongo is a Python distribution containing tools for working with MongoDB. Use the following syntax to use a variable for collection name −var yourVariableName="yourCollectionName"; db[storeCollectionName].yourOperationName;To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows. We have used variable for collection name −> var storeCollectionName="new_Collection"; > db[storeCollectionName].insertOne({"UserName":"John", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912aea4afe5c1d2279d6a0") } > db[storeCollectionName].insertOne({"UserName":"Carol", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912af54afe5c1d2279d6a1") } > db[storeCollectionName].insertOne({"UserName":"Mike", "UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c912afe4afe5c1d2279d6a2") }Display ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
807 Views
To get the database data size in MongoDB, you can use stats() method. The syntax is as follows −db.stats();Let us use the database with the name ‘test’.Now, check the current database with the help of the following query −> db;The following is the output −testHere is the query to get database data size in MongoDB −> db.stats()The following is the output −{ "db" : "test", "collections" : 114, "views" : 0, "objects" : 391, "avgObjSize" : 83.0076726342711, "dataSize" : 32456, "storageSize" : 3211264, "numExtents" : 0, "indexes" : 120, "indexSize" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
257 Views
To find a document with Objectid in MongoDB, use the following syntax −db.yourCollectionName.find({"_id":ObjectId("yourObjectIdValue")}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4384afe5c1d2279d69b") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Mike", "UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4444afe5c1d2279d69c") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Carol", "UserAge":26, "UserHobby":["Learning", "Photography"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4704afe5c1d2279d69d") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findDocumentWithObjectIdDemo.find().pretty();The following is the output ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
977 Views
To avoid duplicate entries in MongoDB, you can use createIndex(). The syntax is as follows −db.yourCollectionName.createIndex({"yourFieldName":1}, {unique:true});Let us implement the above syntax. The query to avoid duplicate entries in MongoDB is a follows −> db.avoidDuplicateEntriesDemo.createIndex({"UserName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Now insert some records in the above collection. The query to insert record is as follows −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e1824afe5c1d2279d697") }Here is the error whenever you try to insert the same record once again −> db.avoidDuplicateEntriesDemo.insertOne({"UserName":"John"}); 2019-03-19T18:03:08.465+0530 E QUERY [js] ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
212 Views
MongoDB is better when you are querying array elements. Let us use the following syntax for querying array elements −db.yourCollectionName.find({yourArrayFieldName:"yourValue"}).pretty();The above syntax will return all those documents which have the value “yourValue” in an array field.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.queryArrayElementsDemo.insertOne({ ... "StudentName":"John", "StudentFavouriteSubject":["C", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90c0354afe5c1d2279d694") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"Carol", "StudentFavouriteSubject":["C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90c0434afe5c1d2279d695") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"David", "StudentFavouriteSubject":["MongoDB", "Java"]}); { "acknowledged" : ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
242 Views
To know which storage engine is used in MongoDB, you can use storageEngine. The syntax is as follows −db.serverStatus().storageEngine;To know all the configuration details of storage engine, you can use the following syntax:db.serverStatus().yourStorageEngineName;Let us implement the above syntax to know which storage engine is being used in MongoDB. The query is as follows −> db.serverStatus().storageEngine;The following is the output −{ "name" : "wiredTiger", "supportsCommittedReads" : true, "supportsSnapshotReadConcern" : true, "readOnly" : false, "persistent" : true }In order to know all configuration details about the above storage engine, the query is as follows −> db.serverStatus().wiredTiger;The following ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
244 Views
To get distinct list of sub-document field values, you can use dot(.). The syntax is as follows −db.yourCollectionName.distinct("yourOuterFieldName.yourInnerFieldName");To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.getDistinctListOfSubDocumentFieldDemo.insertOne( ... { ... "StudentId": 101, ... "StudentPersonalDetails": [ ... { ... "StudentName": "John", ... "StudentAge":24 ... }, ... { ... ... Read More