

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I extract entire documents based on how they compare with their whole collection?
For this, use $$ROOT in MongoDB. Let us create a collection with documents −
> db.demo743.insertOne({id:1,"ShippingDate":"2020-01-21",value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ead893a57bb72a10bcf0680") } > db.demo743.insertOne({id:2,"ShippingDate":"2020-05-10",value:30}); { "acknowledged" : true, "insertedId" : ObjectId("5ead893c57bb72a10bcf0681") } > db.demo743.insertOne({id:3,"ShippingDate":"2020-05-10",value:60}); { "acknowledged" : true, "insertedId" : ObjectId("5ead894657bb72a10bcf0682") } > db.demo743.insertOne({id:1,"ShippingDate":"2020-05-11",value:75}); { "acknowledged" : true, "insertedId" : ObjectId("5ead895657bb72a10bcf0683") }
Display all documents from a collection with the help of find() method −
> db.demo743.find();
This will produce the following output −
{ "_id" : ObjectId("5ead893a57bb72a10bcf0680"), "id" : 1, "ShippingDate" : "2020-01-21", "value" : 50 } { "_id" : ObjectId("5ead893c57bb72a10bcf0681"), "id" : 2, "ShippingDate" : "2020-05-10", "value" : 30 } { "_id" : ObjectId("5ead894657bb72a10bcf0682"), "id" : 3, "ShippingDate" : "2020-05-10", "value" : 60 } { "_id" : ObjectId("5ead895657bb72a10bcf0683"), "id" : 1, "ShippingDate" : "2020-05-11", "value" : 75 }
Following is the query to extract entire documents based on how they compare with their whole collection −
> db.demo743.aggregate([ ... { ... "$project": ... { ... "id": "$id", ... "ShippingDate": "$ShippingDate", ... "MyDoc": "$$ROOT" ... } ... }, ... { ... "$sort": ... { "ShippingDate": -1 ... } ... }, ... { ... "$group": ... { ... "_id":{"id":"$id"}, ... "Result":{"$first":"$MyDoc"} ... } ... }, ... { ... $project: ... { ... "Result.ShippingDate":1, "Result.id":1, "Result.value":1, _id:0} ... } ... ])
This will produce the following output −
{ "Result" : { "id" : 3, "ShippingDate" : "2020-05-10", "value" : 60 } } { "Result" : { "id" : 2, "ShippingDate" : "2020-05-10", "value" : 30 } } { "Result" : { "id" : 1, "ShippingDate" : "2020-05-11", "value" : 75 } }
- Related Questions & Answers
- How can I find documents in MongoDB based on the number of matched objects within an array?
- How can I count the documents in an array based on the value of a specific field?
- How can I aggregate nested documents in MongoDB?
- How to filter documents based on an array in MongoDB?
- How do I change a tag's inner HTML based on their order in JavaScript?
- How can I rename a collection in MongoDB?
- How can I search data from MySQL table based on similar sound values?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to get all the services based on their status in PowerShell?
- How to sort the Processes based on their property name using PowerShell?
- How to select data frame columns based on their class in R?
- How to select data.table object columns based on their class in R?
- How can I search a collection to find a nested value in one of its documents in MongoDB?
- In how many ways can I compare Strings in Java explain with example?
- How can I extract or uncompress gzip file using php?
Advertisements