- 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)
751 Views
Achieve this with aggregation pipeline. Let us first create a collection with documents −> db.demo413.insertOne( ... { ... "_id": "101", ... "details": { ... "Info1": { ... Name:"Chris", ... Age:21 ... }, ... "Info2": { ... Name:"David", ... Age:23 ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() method −> db.demo413.find();This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris", "Age" : 21 }, "Info2" : { "Name" : "David", "Age" : 23 } } }Following is the query to exclude nested fields −> db.demo413.aggregate([ ... { $project: { "details" : { $objectToArray: "$details" } } }, ... { $project: { "details.v.Age" : 0} }, ... { $project: { "details" : { $arrayToObject: "$details"} } } ... ]);This will produce the following output −{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris" }, "Info2" : { "Name" : "David" } } }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
204 Views
Let us create a collection with documents −> db.demo411.aggregate( ... [ ... {$project : { ... _id : 0, ... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ... } ... } ... ] ... ) { "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] } > db.demo412.insertOne( ... { ... "Information1" : [ ... { ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
811 Views
Let us create a collection with documents −> db.demo411.insertOne( ... { ... "Information" : [ ... { ... "Name1" : "Chris", ... "Name2" : "David" ... }, ... { ... "Name1" : "John", ... "Name2" : "John" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
334 Views
To remove elements, use $pull and for such conditions, use $ne. The $ne in MongoDB is used to select the documents where the value of the field is not equal to the specified value.Let us create a collection with documents −> db.demo410.insertOne( ... { ... details: [{isMarried:false}, {isMarried:true}, {isMarried:false}, {isMarried:"Chris"}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e70efc515dc524f70227681") }Display all documents from a collection with the help of find() method −> db.demo410.find();This will produce the following output −{ "_id" : ObjectId("5e70efc515dc524f70227681"), "details" : [ { "isMarried" : false }, { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
148 Views
To find documents with a specific string, use find() and in that search for a string with regex. Let us create a collection with documents −> db.demo409.insertOne({"Name":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e4e515dc524f7022767c") } > db.demo409.insertOne({"Name":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e4ec15dc524f7022767d") } > db.demo409.insertOne({"Name":"Robert Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e4f415dc524f7022767e") } > db.demo409.insertOne({"Name":"David Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e4fe15dc524f7022767f") }Display all documents from a collection with the help of find() method −> db.demo409.find();This will produce the following output −{ "_id" : ObjectId("5e70e4e515dc524f7022767c"), "Name" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
445 Views
To get a query plan in MongoDB, use explain(). The $explain operator gives information on the query plan.Let us create a collection with documents −> db.demo408.insertOne({"Value":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3a115dc524f70227678") } > db.demo408.insertOne({"Value":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3a715dc524f70227679") } > db.demo408.insertOne({"Value":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3ac15dc524f7022767a") } > db.demo408.insertOne({"Value":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3af15dc524f7022767b") } > db.demo408.createIndex({Value:1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Display all documents from a collection with the help ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
201 Views
To create a new user in MongoDB, use createUser(). Following is the query −> db.createUser( ... { ... user: "John", ... pwd: "123456", ... roles: [ { role: "readWrite", db: "test" } ], ... mechanisms: [ "SCRAM-SHA-256" ] ... } ... )This will produce the following output −Successfully added user: { "user" : "John", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] }Following is the query to show all users −> db.getUsers();This will produce the following output −[ { "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] } ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
97 Views
For this, use $setIsSubset in MongoDB. Let us create a collection with documents −> db.demo407.insertOne( ... { ... Name:"Chris", ... "details" : [ ... { ... id:100 ... }, ... { ... id:110 ... }, ... { ... id:130 ... } ... ] ... } ... ); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
88 Views
To aggregate totals, use $sum in MongoDB. Let us create a collection with documents −> db.demo406.insertOne({"Score":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99d5fac4d418a0178599") } > db.demo406.insertOne({"Score":55}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99d8fac4d418a017859a") } > db.demo406.insertOne({"Score":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99dcfac4d418a017859b") } > db.demo406.insertOne({"Score":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99defac4d418a017859c") } > db.demo406.insertOne({"Score":65}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99e3fac4d418a017859d") } > db.demo406.insertOne({"Score":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f99e6fac4d418a017859e") }Display all documents from a collection with the help of find() method −> db.demo406.find();This will ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
114 Views
The find() in MongoDB selects documents in a collection or view and returns a cursor to the selected documents.The find() method with no parameters returns all documents from a collection and returns all fields for the documents. Let us see an example and create a collection with documents −> db.demo405.insertOne({"StudentInfo":{"Name":"Chris"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9134fac4d418a0178595") } > db.demo405.insertOne({"StudentInfo":{"Name":"David"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9138fac4d418a0178596") } > db.demo405.insertOne({"StudentInfo":{"Name":"Bob"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f913cfac4d418a0178597") } > db.demo405.insertOne({"StudentInfo":{"Name":"John"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9140fac4d418a0178598") }Display all documents from a ... Read More