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.