José María
José María

Reputation: 3083

Meteor: retrieve value from document in collection

I have a Collection named "balance". I want to get the value of one document in the collection. In order to get only the latest element in the collection I use this query:

db.balance.find().sort({date: -1}).limit(1);

There's a column called 'value' and I want to get that.

db.balance.find().sort({date: -1}).limit(1).value; however does not show the data I want. It shows nothing:

enter image description here

What's wrong with it?

Upvotes: 0

Views: 65

Answers (1)

David Weldon
David Weldon

Reputation: 64342

find returns a cursor. You'll need to convert it to an array in order to actually extract the value. Try this:

db.balance.find().sort({date: -1}).limit(1).toArray()[0].value;

This is, of course, much easier inside of meteor (either in code or via meteor shell) because you can do:

Balance.findOne({}, {sort: {date: -1}}).value;

Upvotes: 1

Related Questions