Silentbob
Silentbob

Reputation: 3065

Sort by embedded document field

I have the following document

Document Picture

Previously I didn't have the embedded document vdata and I had a field named timestamp and when I ran the query below I got the latest entry.

db.TagValues.aggregate({$match: {tagDetail: "UMIS.99TOTMW.F_CV"}}, {$sort: {timestamp: -1}}, {$limit: 1})

How do I amend the above query to show the same result based on the format of my new document. I have tried the following but I get a syntax error.

db.TagValues.aggregate({$match: {t: "UMIS.99TOTMW.F_CV"}}, {$sort: {vdata.d: -1}}, {$limit: 1})

Upvotes: 1

Views: 136

Answers (1)

Ashh
Ashh

Reputation: 46491

Use double quotes for the nested fields

{ "$sort": {"vdata.d": -1}}

Upvotes: 1

Related Questions