- 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
![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)
260 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
187 Views
You can use positional $ operator. Let us first create a collection with documents −> db.demo22.insertOne( ... { ... ProductId:101, ... ... ProductDetails: ... [ ... { ... ProductFirstPrice: '35', ... ProductSecondPrice: '75' ... }, ... { ... ProductFirstPrice: '', ... ProductSecondPrice:'' ... }, ... { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
215 Views
To update child objects, use $set in MongoDB. Let us first create a collection with documents −>db.demo21.insertOne({"StudentId":"STU-101", "StudentDetails":{"StudentName":"Chris", "StudentAge":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e14be8922d07d3b95082e6f") }Display all documents from a collection with the help of find() method −> db.demo21.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e14be8922d07d3b95082e6f"), "StudentId" : "STU-101", "StudentDetails" : { "StudentName" : "Chris", "StudentAge" : 21 } }Following is the query to update child objects in MongoDB −> db.demo21.update({"StudentId":'STU-101'}, {$set:{'StudentDetails.StudentName':'Robert'}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
89 Views
To shutdown MongoDB, you need to use shutdownServer() as in the below syntax −db.shutdownServer();First you need to switch to admin as shown below −use admin;Here, we switched to admin −> use admin; switched to db adminFollowing is the query to shutdown server −> db.shutdownServer();This will produce the following output −server should be down... 2020-01-07T22:40:31.295+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2020-01-07T22:40:32.326+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
350 Views
To get the number of open connections, use serverStatus() in MongoDB. Following is the query −> db.serverStatus();This will produce the following output −{ "host" : "DESKTOP-QN2RB3H", "version" : "4.0.5", "process" : "mongod", "pid" : NumberLong(10540), "uptime" : 74156, "uptimeMillis" : NumberLong(74156688), "uptimeEstimate" : NumberLong(74156), "localTime" : ISODate("2020-01-07T15:09:18.366Z"), "asserts" : { "regular" : 0, "warning" : 0, "msg" : 0, "user" : 1, "rollovers" : 0 }, "connections" : { "current" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
155 Views
You can use $unset. Let us first create a collection with documents −> db.demo20.insertOne( ... { ... ... "ListOfEmployee" : [ ... { ... "EmployeeName1" : "John" ... }, ... { ... "EmployeeName2" : "Carol" ... } ... ], ... "EmployeeName2" : [] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e138c3555d0fc6657d21f12") }Following is the query to ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
464 Views
To filter records using Regular Expression in MongoDB, use $regex. Let us first create a collection with documents −> db.demo19.insertOne({"Values":"4321GH"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389b955d0fc6657d21f0f") } > db.demo19.insertOne({"Values":"12321_Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389c755d0fc6657d21f10") } > db.demo19.insertOne({"Values":"8765Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1389d355d0fc6657d21f11") }Following is the query to display all documents from a collection with the help of find() method −> db.demo19.find();This will produce the following output −{ "_id" : ObjectId("5e1389b955d0fc6657d21f0f"), "Values" : "4321GH" } { "_id" : ObjectId("5e1389c755d0fc6657d21f10"), "Values" : "12321_Carol" } { "_id" : ObjectId("5e1389d355d0fc6657d21f11"), "Values" : "8765Mike" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
164 Views
To convert a field to an array, use UPDATE operation inside forEach(). Let us first create a collection with documents −> db.demo18.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e1387fc55d0fc6657d21f0e") }Following is the query to display all documents from a collection with the help of find() method −> db.demo18.find();This will produce the following output −{ "_id" : ObjectId("5e1387fc55d0fc6657d21f0e"), "StudentName" : "John" }Here is the query to convert a field to an array using update operation −> db.demo18.find().forEach(function(myDocument) { ... db.demo18.update( ... { _id: myDocument._id }, ... { "$set": { "StudentName": [myDocument.StudentName] } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
260 Views
For this, use $all, that would find documents containing all of the elements in an array like "keys". Let us first create a collection with documents −> db.demo17.insertOne({"ListOfSubject":["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e13847255d0fc6657d21f0a") } > db.demo17.insertOne({"ListOfSubject":["C", "Python", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e13847e55d0fc6657d21f0b") } > db.demo17.insertOne({"ListOfSubject":["C++", "MongoDB", "PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e13849255d0fc6657d21f0c") }Following is the query to display all documents from a collection with the help of find() method −> db.demo17.find();This will produce the following output −{ "_id" : ObjectId("5e13847255d0fc6657d21f0a"), "ListOfSubject" : [ "MySQL", "MongoDB", ... Read More