- 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
124 Views
To speed up the $group phase, use $group along with aggregation. Let us see an example and create a collection with documents −> db.demo423.insertOne({"Information":[101, 110, 87, 110, 98, 115, 101, 115, 89, 115]}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a60e9822da45b30346e6") }Display all documents from a collection with the help of find() method −> db.demo423.find();This will produce the following output −{ "_id" : ObjectId("5e73a60e9822da45b30346e6"), "Information" : [ 101, 110, 87, 110, 98, 115, 101, 115, 89, 115 ] }Following is the query to speed up $group phase in aggregation −> db.demo423.aggregate([ ... { ... $project: ... Read More
623 Views
For exact match, set the values to be matched inside MongoDB $in(). Let us first create a collection with documents −> db.demo422.insertOne({"Name":"Chris", "Marks":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4059822da45b30346e1") } > db.demo422.insertOne({"Name":"Chris", "Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a40a9822da45b30346e2") } > db.demo422.insertOne({"Name":"David", "Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4149822da45b30346e3") } > db.demo422.insertOne({"Name":"Sam", "Marks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a41e9822da45b30346e4") } > db.demo422.insertOne({"Name":"David", "Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e73a4239822da45b30346e5") }Display all documents from a collection with the help of find() method −> db.demo422.find();This will produce ... Read More
974 Views
To insert date in MongoDB, use Date(). Let us create a collection with documents −> db.demo421.insert({"DueDate":new Date(Date.now())}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2020-01-15")}); WriteResult({ "nInserted" : 1 }) > db.demo421.insert({"DueDate":new Date("2018-12-31")}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo421.find();This will produce the following output −{ "_id" : ObjectId("5e73a2ec9822da45b30346de"), "DueDate" : ISODate("2020-03-19T16:50:52.746Z") } { "_id" : ObjectId("5e73a2fb9822da45b30346df"), "DueDate" : ISODate("2020-01-15T00:00:00Z") } { "_id" : ObjectId("5e73a3079822da45b30346e0"), "DueDate" : ISODate("2018-12-31T00:00:00Z") }Read More
420 Views
To combine unique items from array, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo420.insert( ... { ... ... "details" : [ ... { ... "Value1":10, ... "Value2":20, ... "Value3":30 ... } ... ] ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo420.insert( ... { ... ... "Info" : [ ... { ... Read More
683 Views
Let us first create a collection with documents −>db.demo419.insertOne({"ProductInformation":[{"ProductName":"Product-1", "ProductPrice":500}, {"ProductName":"Product-2", "ProductPrice":600}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e724762b912067e57771ae8") }Display all documents from a collection with the help of find() method −> db.demo419.find();This will produce the following output −{ "_id" : ObjectId("5e724762b912067e57771ae8"), "ProductInformation" : [ { "ProductName" : "Product-1", "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductPrice" : 600 } ] }Following is the query to update object in array with a specific key −> db.demo419.update({'ProductInformation.ProductName' : "Product-1" }, { $set : { 'ProductInformation.$.ProductPrice' : 1250}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }Display ... Read More
637 Views
For this, use aggregate(). Let us first create a collection with documents −> db.demo418.insertOne( ... { ... "details":[ ... { ... "CountryName":"US", ... "Marks":45 ... }, ... { ... "CountryName":"US", ... "Marks":56 ... }, ... ... ], ... "Name":"John" ... } ... ); { "acknowledged" : true, ... Read More
811 Views
Let us first create a collection with documents −> db.demo417.insertOne({"EmployeeName":"Chris", "EmployeeSalary":500}); { "acknowledged" : true, "insertedId" : ObjectId("5e723ebbb912067e57771ae4") } > db.demo417.insertOne({"EmployeeName":"Mike", "EmployeeSalary":1000}); { "acknowledged" : true, "insertedId" : ObjectId("5e723ed7b912067e57771ae5") }Display all documents from a collection with the help of find() method −> db.demo417.find();This will produce the following output −{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 500 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1000 }Following is the query to update salary field value with 10 percent of each employee in employee collection −> db.demo417.update({ "EmployeeName": { $in: ["Chris", "Mike"] } }, ... Read More
170 Views
To convert collection to capped, use runCommand(). It provides a helper to run specified database commands. Let us first create a collection with documents −> db.demo416.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e723a7bb912067e57771adf") } > db.demo416.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e723a81b912067e57771ae0") } > db.demo416.insertOne({"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e723a84b912067e57771ae1") }Display all documents from a collection with the help of find() method −> db.demo416.find();This will produce the following output −{ "_id" : ObjectId("5e723a7bb912067e57771adf"), "StudentName" : "David" } { "_id" : ObjectId("5e723a81b912067e57771ae0"), "StudentName" : "Mike" } { "_id" : ObjectId("5e723a84b912067e57771ae1"), "StudentName" : ... Read More
7K+ Views
To select specific columns, you can ignore the rest of them i.e. to hide those columns, set them to 0. Let us first create a collection with documents −> db.demo415.insertOne({"ClientName":"Robert", "ClientCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e72329db912067e57771adc") } > db.demo415.insertOne({"ClientName":"David", "ClientCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e7232acb912067e57771add") } > db.demo415.insertOne({"ClientName":"Bob", "ClientCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e7232b4b912067e57771ade") }Display all documents from a collection with the help of find() method −> db.demo415.find();This will produce the following output −{ "_id" : ObjectId("5e72329db912067e57771adc"), "ClientName" : "Robert", "ClientCountryName" : "US" } { "_id" : ObjectId("5e7232acb912067e57771add"), ... Read More
697 Views
To find by _id on an array of objects, use aggregation and avoid using find(). Let us first create a collection with documents −> db.demo414.insertOne( ... { ... "_id": "110", ... "details":[ ... { ... "StudentName":"John", ... "StudentMarks":56 ... }, ... { ... "StudentName":"Robert", ... "StudentMarks":98 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the help of find() method −> db.demo414.find();This will produce the following output −{ "_id" : "110", "details" : [ { "StudentName" : "John", "StudentMarks" : 56 }, { "StudentName" : "Robert", "StudentMarks" : 98 } ] }Following is the query to find by _id on an array of objects −> db.demo414.aggregate([{$unwind: "$details"}, {$match:{"details.StudentMarks" :56}}] )This will produce the following output −{ "_id" : "110", "details" : { "StudentName" : "John", "StudentMarks" : 56 } }