0

I have the following record in my DB [Simplified to what matters to the question], I was wondering if there is a way where I can select all records with Type = 1, customerID = "Customer 1" and sort by transDateTime ASC? Thanks.

{
    type: "1",
    notes: "bla bla",
    transactions : [ 
        {
            "customerID" : "Customer 1",
            "transDateTime" : ISODate("2017-09-10T18:54:41.983Z")
        }, 
        {
            "transDateTime" : ISODate("2017-08-10T18:54:41.983Z")
        },
        {
            "customerID" : "Customer 1",
            "transDateTime" : ISODate("2017-02-10T11:54:41.983Z")
        }    
    ],
}
Mahmoud Metwally
  • 980
  • 1
  • 10
  • 24
  • 1
    Possible duplicate of [how to sort array inside collection record in mongoDB](https://stackoverflow.com/questions/13449874/how-to-sort-array-inside-collection-record-in-mongodb) – s7vr Jan 03 '18 at 00:55

1 Answers1

0

You can access sub-fields via a dot notation

find({ "type": "1", "transactions.customerID": "Customer 1" }).sort({ "transactions.transDateTime": 1  })

https://docs.mongodb.com/v3.2/core/document/#document-dot-notation

Daniel Khoroshko
  • 2,623
  • 15
  • 26