MongoDb Queries
- Group By Query: grouped using ChassisNo
[
{โ$groupโ : { โ_idโ: โ$chassisNoโ, โcountโ: { โ$sumโ: 1 } } },
{โ$matchโ: {โ_idโ :{ โ$neโ : null } , โcountโ : {โ$gtโ: 1} } },
{โ$projectโ: {โnameโ : โ$_idโ, โ_idโ : 0} }
]
Grouped using $group by ChassisNo,
In match we specify id shouldnโt be null and we take the group items which is more than one in count(โcountโ : {โ$gtโ: 1} )
In Projection, we assign it to the name key and _id which is default field we donโt need that so we give _id:0
2. Select one field in MongoDb: ==> {}, {invoiceNo:1, _id:0}
db.getCollection(โt_frwrdr_invcโ).find({}, {invoiceNo:1, _id:0}).sort({ โ_idโ : -1})
{}-> match condition
{invoiceNo:1, _id:0} -> Result set extraction.
InvoiceNo only printed in the result.
_id : which is common for all the documents. So when we give _id:0 then it will not be shown in document. if we have given _id:1 then it will be shown in the result set.