Question

I have got records in my collection as shown below

{ 
 "_id" : ObjectId("53722c39e4b04a53021cf3c6"), 
 "symbol" : "AIA", 
 "tbq" : 1356, 
 "tsq" : 0, 
 "tquan" : 6831336, 
 "tvol" : 17331.78, 
 "bquantity" : 1356, 
 "squantity" : 0 
}

{ 
 "_id" : ObjectId("53722c38e4b04a53021cf3c1"), 
 "symbol" : "SAA", 
 "tbq" : 0, 
 "tsq" : 9200, 
 "tquan" : 6036143, 
 "tvol" : 50207.43, 
 "bquantity" : 0, 
 "squantity" : 9200 
}

I am displaying the results in the ascending order of bquantity, and also at the same time I want to display only certain columns in the result (symbol, bquantity, squantity) and ignore the rest.

I tried with the below query, but still its displaying all the fields .

Please tell me how can i eliminate those fields from the result ?

db.stock.find().sort(
                     {"bquantity":-1}, 
                     { 
                       symbol: 1, 
                       bquantity: 1, 
                       squantity:1 ,
                       _id:0,
                       tbq:0,
                       tsq:0,
                       tquan:0,
                       tvol:0
                      } 
                    )
Was it helpful?

Solution

The field filter is a parameter to the find function, not to the sort function, i.e.:

db.stock.find({}, { _id:0,tbq:0, tsq:0,tquan:0,tvol:0}).sort({"bquantity":-1})

The empty hash used as first parameter to find is required as an 'empty query' which matches all documents in stock.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top