简体   繁体   中英

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:

在此处输入图片说明

What's wrong with it?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM