- 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
159 Views
Use $unwind twice for specific embedded document in MongoDB. Let us create a collection with documents −> db.demo631.insert( ... { ... id: "101", ... Info1: [ ... { ... CountryName : "US", ... Info2 : [ ... { ... Name:"Chris", ... Age:24 ... }, { ... Read More
235 Views
To sort documents, use sort() along with find(). Let us create a collection with documents −> db.demo630.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5e9b080e6c954c74be91e6ba") } > db.demo630.insertOne({"Value":200}); { "acknowledged" : true, "insertedId" : ObjectId("5e9b08116c954c74be91e6bb") } > db.demo630.insertOne({"Value":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e9b08146c954c74be91e6bc") } > db.demo630.insertOne({"Value":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e9b08176c954c74be91e6bd") }Display all documents from a collection with the help of find() method −> db.demo630.find();This will produce the following output −{ "_id" : ObjectId("5e9b080e6c954c74be91e6ba"), "Value" : 10 } { "_id" : ObjectId("5e9b08116c954c74be91e6bb"), "Value" : 200 } { "_id" : ... Read More
138 Views
You can use $match. The $match filters the documents to pass only the documents that match the specified condition to the next pipeline stage. Let us create a collection with documents −> db.demo629.insertOne( ... { ... ... "Subject": [ ... "MySQL", ... "MongoDB" ... ], ... "details": [ ... { ... Name:"Chris", ... "Marks":78 ... }, ... { ... Read More
77 Views
To query for values in list, use positional operator($) in MongoDB. Let us create a collection with documents −> db.demo628.insertOne({id:1, Name:["Chris", "David", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ae7ea6c954c74be91e6b6") } > db.demo628.insertOne({id:1, Name:["Carol", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ae7f26c954c74be91e6b7") } > db.demo628.insertOne({id:2, Name:["Mike", "Sam", "John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ae8056c954c74be91e6b8") }Display all documents from a collection with the help of find() method −> db.demo628.find();This will produce the following output −{ "_id" : ObjectId("5e9ae7ea6c954c74be91e6b6"), "id" : 1, "Name" : [ "Chris", "David", "John" ] } { "_id" : ObjectId("5e9ae7f26c954c74be91e6b7"), "id" ... Read More
692 Views
For this, use aggregate() along with $group. Let us create a collection with documents −> db.demo627.insertOne({id:101, "Name":"Chris", "Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb306c954c74be91e6b2") } > db.demo627.insertOne({id:102, "Name":"Bob", "Marks":74}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb3c6c954c74be91e6b3") } > db.demo627.insertOne({id:101, "Name":"Chris", "Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb426c954c74be91e6b4") } > db.demo627.insertOne({id:102, "Name":"Mike", "Marks":70}); { "acknowledged" : true, "insertedId" : ObjectId("5e9acb4b6c954c74be91e6b5") }Display all documents from a collection with the help of find() method −> db.demo627.find();This will produce the following output −{ "_id" : ObjectId("5e9acb306c954c74be91e6b2"), "id" : 101, "Name" : "Chris", "Marks" : ... Read More
241 Views
Remove document using remove(), whose value is matched with $eq from a MongoDB collection. The $eq operator matches documents where the value of a field equals the specified value.Let us create a collection with documents −> db.demo626.insertOne({id:1, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6376c954c74be91e6ae") } > db.demo626.insertOne({id:2, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac63e6c954c74be91e6af") } > db.demo626.insertOne({id:3, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6436c954c74be91e6b0") } > db.demo626.insertOne({id:4, "Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ac6486c954c74be91e6b1") }Display all documents from a collection with the help of find() method −> db.demo626.find();This ... Read More
389 Views
For array interection in MongoDB, use the $setIntersection in aggregate(). Let us create a collection with documents −> db.demo625.insertOne( ... { ... Name: "John", ... Marks: [56, 98, 60] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e16c954c74be91e6aa") } > db.demo625.insertOne( ... { ... Name: "John", ... Marks: [110, 56, 72] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ab") } > db.demo625.insertOne( ... { ... Name: "Chris", ... ... Read More
80 Views
For all elements of a field in MongoDB, use find() and in that, use $elemMatch. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo624.insertOne({"ListOfName":["John", "Chris", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab3ff6c954c74be91e6a5") } > db.demo624.insertOne({"ListOfName":["John", "Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab4026c954c74be91e6a6") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab4076c954c74be91e6a7") } > db.demo624.insertOne({"ListOfName":["John", "Chris", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e9ab40e6c954c74be91e6a8") } > ... Read More
406 Views
Yes, to search an array of objects, use $unwind in MongoDB aggregate(). To match, use $match. Let us create a collection with documents −> db.demo623.insertOne( ... { ... _id:1, ... details:[ ... { ... Name:"Chris" ... }, ... { ... DueDate:new ISODate("2020-01-10") ... }, ... { ... CountryName:"US" ... } ... ... Read More
46 Views
Yes, you can use ensureIndex(). MongoDB provides complete support for indexes on any field in a collection of documents.Let us create a collection with documents −> db.demo622.ensureIndex({_id:1, Name:1, Age:1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo622.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo622.insertOne({_id:102, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 102 } > db.demo622.insertOne({_id:103, Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : 103 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 104 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); 2020-04-18T12:21:18.085+0530 ... Read More