- 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 6702 Articles for Database
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
125 Views
To convert an existing collection to capped, use convertToCapped. Let us create a collection with documents −> db.demo260.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47b1181627c0c63e7dba9b") } > db.demo260.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47b11c1627c0c63e7dba9c") } > db.demo260.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47b11f1627c0c63e7dba9d") }Display all documents from a collection with the help of find() method −> db.demo260.find();This will produce the following output −{ "_id" : ObjectId("5e47b1181627c0c63e7dba9b"), "Name" : "Chris" } { "_id" : ObjectId("5e47b11c1627c0c63e7dba9c"), "Name" : "Bob" } { "_id" : ObjectId("5e47b11f1627c0c63e7dba9d"), "Name" : "David" }Following is the query to invoke convertToCapped ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
413 Views
To get documents except some specific documents, use $nor along with $and. Let us first create a collection with documents −> db.demo1.insertOne({"StudentName":"Chris", "StudentMarks":38}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f025ddae1f53b62216") } > db.demo1.insertOne({"StudentName":"David", "StudentMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f725ddae1f53b62217") } > db.demo1.insertOne({"StudentName":"Mike", "StudentMarks":96}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4fd25ddae1f53b62218") }Following is the query to display all documents from a collection with the help of find() method −> db.demo1.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e08a4f025ddae1f53b62216"), "StudentName" : "Chris", "StudentMarks" : 38 } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
79 Views
To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is db.collectionName.find().Let us create a collection with documents −> db.demo259.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ae1f1627c0c63e7dba98") } > db.demo259.insertOne({"Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ae231627c0c63e7dba99") } > db.demo259.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ae281627c0c63e7dba9a") }Display all documents from a collection with the help of find() method −> db.demo259.find();This will produce the following output −{ "_id" : ObjectId("5e47ae1f1627c0c63e7dba98"), "Subject" : "MySQL" } { "_id" : ObjectId("5e47ae231627c0c63e7dba99"), "Subject" : "Java" } { "_id" : ObjectId("5e47ae281627c0c63e7dba9a"), "Subject" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
587 Views
To pull value from array of ObjectIDs, use $pull in MongoDB. Let us create a collection with documents −> db.demo258.insertOne({"arrayOfObjectsId":[ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91")]}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a8211627c0c63e7dba97") }Display all documents from a collection with the help of find() method −> db.demo258.find();This will produce the following output −{ "_id" : ObjectId("5e47a8211627c0c63e7dba97"), "arrayOfObjectsId" : [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ] }Following is the query to pull value from array of ObjectIDs −> db.demo258.update( { }, { $pull: { arrayOfObjectsId: { $in: [ ObjectId("5e47a5e81627c0c63e7dba92") ] } } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
514 Views
To sum, use $sum and to get sum of specific documents, you need to group them using $group in MongoDB.Let us first create a collection with documents −>db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol", "Bob"], "UsersDetails":[{"FirstUser":"Carol", "TotalLikes":20}, {"FirstUser":"Bob", "TotalLikes":45}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e084e9125ddae1f53b6220c") } >db.calculateSumOfDocument.insertOne({"ListOfUsers":["Carol", "Bob"], "UsersDetails":[{"FirstUser":"Carol", "TotalLikes":60}, {"FirstUser":"Bob", "TotalLikes":50}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e084f6125ddae1f53b6220d") }Following is the query to display all documents from a collection with the help of find() method −> db.calculateSumOfDocument.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e084e9125ddae1f53b6220c"), "ListOfUsers" : [ "Carol", "Bob" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
175 Views
To set unique index in MongoDB, use unique:true. Let us create a collection with documents −> db.demo257.ensureIndex({Name:1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo257.insertOne({Name:"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a5e51627c0c63e7dba91") } > db.demo257.insertOne({Name:"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a5e81627c0c63e7dba92") } > db.demo257.insertOne({Name:"Chris"}); 2020-02-15T13:33:54.064+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo257 index: Name_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
128 Views
For this, we will use the concept of createIndex() and create index −> db.compoundIndexDemo.createIndex({"StudentName":1, "StudentAge":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Let us first create a collection with documents −> db.compoundIndexDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e084c1d25ddae1f53b62207") } > db.compoundIndexDemo.insertOne({"StudentName":"Chris", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e084c5625ddae1f53b62209") } > db.compoundIndexDemo.insertOne({"StudentName":"Chris", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e084c5b25ddae1f53b6220a") } > db.compoundIndexDemo.insertOne({"StudentName":"Chris", "StudentAge":23}); 2019-12-29T12:19:02.225+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: web.compoundIndexDemo index: StudentName_1_StudentAge_1 dup key: { : "Chris", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
407 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" : true, "insertedId" : ObjectId("5e47a3eb1627c0c63e7dba8d") }Display all documents from a collection with the help of find() method −> db.demo256.find();This will produce the following output −{ "_id" : ObjectId("5e47a3e91627c0c63e7dba8b"), "Name" : "Chris" } { "_id" : ObjectId("5e47a3ea1627c0c63e7dba8c"), "Name" : "Bob" } { "_id" : ObjectId("5e47a3eb1627c0c63e7dba8d"), "Name" : "David" }Following is ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
171 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" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo255.find();This will produce the following output −{ "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
314 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 −{ "_id" : ObjectId("5e0849d925ddae1f53b62206"), "StudentName" : "John Smith" }Here is the query to split a string −> db.splitString.mapReduce( ... function() { ... var StudentLastName = this.StudentName.split(/\s/).reverse()[0].toUpperCase(); ... ... emit({ "StudentLastName": StudentLastName, "FirstObjectId": this._id }, this); ... }, ... function(){}, ... ... Read More