- 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)
59 Views
Let us create a collection with documents −> db.demo324.insertOne({"ListOfValues":[10, 20, 30]}); { "acknowledged" : true, "insertedId" : ObjectId("5e516349f8647eb59e562073") }Display all documents from a collection with the help of find() method −> db.demo324.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e516349f8647eb59e562073"), "ListOfValues" : [ 10, 20, 30 ] }Here is the query displaying how to use positional operator −> db.demo324.update({"ListOfValues":[10, 20, 30]}, { $set: { "ListOfValues.$[]": "MongoDB" } }, { upsert: true } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
89 Views
At first, let us create a collection with documents and also use ensureIndex() to create an index −> db.demo323.insertOne({"details":{"Name":"Chris", "Age":34}}); { "acknowledged" : true, "insertedId" : ObjectId("5e51157af8647eb59e56206e") } > db.demo323.insertOne({"details":{"Name":"David", "Age":31}}); { "acknowledged" : true, "insertedId" : ObjectId("5e511581f8647eb59e56206f") } > db.demo323.insertOne({"details":{"Name":"Bob", "Age":28}}); { "acknowledged" : true, "insertedId" : ObjectId("5e511589f8647eb59e562070") } > db.demo323.ensureIndex({"details.Name":1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 }Display all documents from a collection with the help of find() method −> db.demo323.find();This will produce the following output −{ "_id" : ObjectId("5e51157af8647eb59e56206e"), "details" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
179 Views
To check for existing documents/embedded documents, use $exists in MongoDB. Let us create a collection with documents −> db.demo322.insertOne( ... {'id':1001, ... 'details':[{'Score':10000, Name:"Bob"}, ... {'Score':98000, Name:"Sam"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113e2f8647eb59e56206c") } > db.demo322.insertOne( ... {'id':10002, ... 'details':[{'Score':9000}, ... {'Score':91000} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113faf8647eb59e56206d") }Display all documents from a collection with the help of find() method −> db.demo322.find();This will produce the following output −{ "_id" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Let us first create a collection with documents wherein we are storing array values −>db.demo321.insertOne({"UserDetails":[{"UserId":101, "UserName":"Chris"}, {"UserId":102, "UserName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511248f8647eb59e56206a") } >db.demo321.insertOne({"UserDetails":[{"UserId":103, "UserName":"Bob"}, {"UserId":104, "UserName":"Sam"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511259f8647eb59e56206b") }Display all documents from a collection with the help of find() method −> db.demo321.find();This will produce the following output −{ "_id" : ObjectId("5e511248f8647eb59e56206a"), "UserDetails" : [ { "UserId" : 101, "UserName" : "Chris" }, { "UserId" : 102, "UserName" : "Mike" } ] } { "_id" : ObjectId("5e511259f8647eb59e56206b"), "UserDetails" : [ { "UserId" : 103, "UserName" : "Bob" }, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
146 Views
The fastest way of updating is update(). Let us create a collection with documents and see how update() works −> db.demo320.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee51f8647eb59e562066") } > db.demo320.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee55f8647eb59e562067") } > db.demo320.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee59f8647eb59e562068") } > db.demo320.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ee5bf8647eb59e562069") }Display all documents from a collection with the help of find() method −> db.demo320.find();This will produce the following output −{ "_id" : ObjectId("5e50ee51f8647eb59e562066"), "Name" : "Chris" } { "_id" : ObjectId("5e50ee55f8647eb59e562067"), "Name" : "Robert" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
334 Views
In MongoDB, you cannot use capped for sub-collection. However, use capped on the overall document. To display a specific number of values from an array, prefer $slice.Let us create a collection with documents −> db.demo319.insertOne({"Scores":[100, 345, 980, 890]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ecf6f8647eb59e562064") } > db.demo319.insertOne({"Scores":[903, 10004, 84575, 844]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ed01f8647eb59e562065") }Display all documents from a collection with the help of find() method −> db.demo319.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e50ecf6f8647eb59e562064"), "Scores" : [ 100, 345, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
403 Views
To pull a specific value, use UPDATE with $pull. Let us create a collection with documents −> db.demo318.insertOne({Subject:["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea6df8647eb59e562062") } > db.demo318.insertOne({Subject:["Spring", "Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea78f8647eb59e562063") }Display all documents from a collection with the help of find() method −> db.demo318.find();This will produce the following output −{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring", "Hibernate" ] }Following is the query to $pull a specific value “Hibernate” −> db.demo318.update({Subject:"Hibernate"}, {$pull:{"Subject":"Hibernate"}}); WriteResult({ "nMatched" : 1, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
240 Views
To get values greater than a specific value, use $gt along with find(). Let us create a collection with documents −> db.demo317.insertOne( ... {'id':101, ... 'details':[{'Score':78, Name:"Chris"}, ... {'Score':88, Name:"David"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e50e69cf8647eb59e562060") } > db.demo317.insertOne( ... {'id':102, ... 'details':[{'Score':90, Name:"Chris"}, ... {'Score':91, Name:"David"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e50e6adf8647eb59e562061") }Display all documents from a collection with the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
140 Views
For this, use ensureIndex() and set unique:true. Let us create a collection with documents. Here, when we try to inert duplicate item, then a duplicate key error occurs −> db.demo316.ensureIndex({"SubjectName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo316.insertOne({"SubjectName":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e378f8647eb59e56205d") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e37df8647eb59e56205e") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); 2020-02-22T13:47:05.186+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo316 index: SubjectName_1 dup key: { : "MongoDB" } : WriteError({ "index" : 0, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
261 Views
To update the nested document, use $set. Let us create a collection with documents −> db.demo315.insertOne({ _id :101, ... details: [ ... {Name: 'Chris', subjects: [{id:1001, SubjectName:"MySQL"}]} ... ] ... } ...) { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo315.find().pretty();This will produce the following output −{ "_id" : 101, "details" : [ { "Name" : "Chris", "subjects" : [ { ... Read More