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.