0

I wonder if I can sort and find in Mongo but return only specific fields.

This command sorts the results by the field properties.aar but returns all the fields from the dataset.

db.samplecol.find().sort({"properties.aar":1})

Result (an example):

{ "_id" : ObjectId("5ad70f71f2119236741ffb03"), "geometry" : { "type" : "Point", "coordinates" : [ 119.6164, 4.7121 ] }, "type" : "Feature", "properties" : { "STATUS" : "0", "TIMESTAMP" : "2013-12-31 18:49:00.000", "aar" : "205580000", "COURSE" : "175", "SPEED" : "153", "HEADING" : "176" } }

I wonder if I can return only the geometry.coordinates.

Update

The results aren't sorted based on the field properties.aar. The code that works for me is (pymongo):

db.samplecol.find({}, {"properties.aar":1,"geometry.coordinates":1, "_id":0}).sort([("properties.aar", pymongo.ASCENDING)])

So, @Neil Lunn please don't judge so fast to mark a question.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
e7lT2P
  • 1,635
  • 5
  • 31
  • 57

1 Answers1

0

What you are looking for is called 'projection' :

Check out the documentation here.

A projection can explicitly include several fields by setting the to 1 in the projection document. The following operation returns all documents that match the query. In the result set, only the item, status and, by default, the _id fields return in the matching documents.

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

If you are using mongoose, you can use the .select() method, more info here.

Community
  • 1
  • 1
HRK44
  • 2,382
  • 2
  • 13
  • 30