- 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
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
509 Views
You need to use insert() for this. Whenever you insert custom _id values and the document already exist with the custom _id value then an error is visible. Let us first create a collection with documents. Under this, we tried adding the same document again and this resulted in an error> db.customIdDemo.insert({"_id":1, "StudentName":"John"}); WriteResult({ "nInserted" : 1 }) > db.customIdDemo.insert({"_id":1, "StudentName":"Carol"}); WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: admin.customIdDemo index: _id_ dup key: { : 1.0 }" } }) > db.customIdDemo.insert({"_id":2, "StudentName":"Carol"}); ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
96 Views
You can use buidInfo along with runCommand to check MongoDB for 32 bits or 64 bits. First switch your database to admin. Following is the syntaxuse adminAfter that use the following syntax to know if my server runs MongoDB 64 bits or notdb.runCommand(buildInfo)Now execute the above syntax> use admin switched to db admin > db.runCommand("buildInfo");Following is the output displaying MongoDB is 64 bits{ "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412", "targetMinOS" : "Windows 7/Windows Server 2008 R2", "modules" : [ ], "allocator" : "tcmalloc", "javascriptEngine" : "mozjs", "sysInfo" : "deprecated", "versionArray" : [ ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
606 Views
Access objects using dot notation. Let us first create a collection with documents> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], ... "StudentCountryName" : [ "US" ], ... "StudentCoreSubject" : [ "C", "Java" ], ... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }); { "acknowledged" : true, "insertedId" : ObjectId("5c99dfc2863d6ffd454bb650") }Following is the query to display all documents from a collection with the help of find() method> db.nestedObjectDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c99dfc2863d6ffd454bb650"), "Student" : { "StudentDetails" : ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
521 Views
To search multiple fields for multiple values in MongoDB, you can use $text and $search operator. Let us first create a collection with documents>db.searchMultipleFieldsDemo.insertOne({"_id":100, "FirstSubject":"Java", "SecondSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : 100 } >db.searchMultipleFieldsDemo.insertOne({"_id":101, "FirstSubject":"MongoDB", "SecondSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : 101 } >db.searchMultipleFieldsDemo.insertOne({"_id":102, "FirstSubject":"MySQL", "SecondSubject":"Java"}); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method> db.searchMultipleFieldsDemo.find().pretty();This will produce the following output{ "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" } { ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
768 Views
Use aggregate() method to get all the non-distinct values of a field. Let us first create a collection with documents> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":25}); ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
123 Views
To create an index in MongoDB, use the ensureIndex() method. Let us first create a collection using following query> db.createCollection(&qu/ot;creatingUniqueIndexDemo"); { "ok" : 1 }Following is the query to create an index on the above collection:> db.creatingUniqueIndexDemo.ensureIndex({"UserCountryName":1}, {unique:true}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Following is the query to insert some documents in the above collection>db.creatingUniqueIndexDemo.insertOne({"UserName":"John", "UserAge":21, "UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947bd330fd0aa0d2fe4d8") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Mike", "UserAge":23, "UserCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9947c9330fd0aa0d2fe4d9") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"US"}); 2019-03-26T02:57:52.670+0530 E QUERY [js] WriteError: ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
190 Views
Following is the syntax to drop all indexes from all collections in a MongoDB database using command linedb.getCollectionNames().forEach(function(yourVariableName) { db.runCommand({dropIndexes: yourVariableName, index: "*"}); });The above syntax will drop all indexes except _id.Let us check the current database. Following is the query> dbThis will produce the following outputTestFollowing is the query to let us show some indexes from a collection before dropping indexes> db.indexingDemo.getIndexes();This will produce the following output[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
68 Views
To get all the MongoDB documents with some given criteria’s, following any of the below given casesCase 1 Following is the query to get all the documents without a single criterion using $ne operatordb.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();Case 2 Following is the query to get all the documents without two given criterions using $nin operatordb.yourCollectionName.find({yourFieldName:{$nin:["yourValue1", "yourValue2"]}}).pretty();Let us first create a collection. Following is the query to create a collection with documents>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry", "StudentSubjectName":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris", "StudentSubjectName":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert", "StudentSubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4") } ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
3K+ Views
Let us first create a collection. Following is the query to create a collection with documents> db.documentExistsOrNotDemo.insertOne({"UserId":101, "UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932bd330fd0aa0d2fe4cf") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932c6330fd0aa0d2fe4d0") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9932ce330fd0aa0d2fe4d1") }Following is the query to display all the documents from a collection with the help of find() method> db.documentExistsOrNotDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9932bd330fd0aa0d2fe4cf"), "UserId" : 101, "UserName" : "John" } { "_id" : ObjectId("5c9932c6330fd0aa0d2fe4d0"), "UserId" : 102, ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
84 Views
To clear items in a nested array, use the $set operator. Let us first create a collection. Following is the query to create a collection with documents> db.clearingItemsInNestedArrayDemo.insertOne( { ... ... "StudentName" : "John", ... "StudentDetails" : [ ... { ... "ProjectName" : "Online Banking", ... "ProjectDetails" : [ ... { ... "TechnologyUsed" : "Java", ... "TeamSize":5 ... }, ... ... ... Read More