0

I have a document where stock prices are saved in an embedded document within an array in MongoDB. I need to get one stock price, depending on the date, and both display in the template and also use in further calculations.

I have a Meteorpad with an example. The helper to get the stock price starts at /client/app.js line 25, using the code from @Hakan Kose's answer. Not sure how to change the last line though (console.log won't work here).

For 2015-12-01, the query should return 117.34.

{
  ticker: "AAPL",
  valuationDate: "2015-12-01",
  closingPrices: [
    {date: "2015-12-01", close: "117.34"},
    {date: "2015-12-02", close: "116.28"},
    {date: "2015-12-03", close: "115.20"},
    {date: "2015-12-04", close: "119.03"}
  ]
}

Thank you for any assistance.

1 Answer 1

1

You can do this easyly with;

Valuations.findOne({_id:this._id}, function(data){
   data.closingPrices.forEach(function(closingPrices){
       if(closingPrices.date === valuationDate){
           console.log(closingPrices.close)
       }
   });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much. Novice question here - your last line uses console.log but I'm trying to display the value in the template and use in a calculation. I added your code to the Meteorpad but how can I change the last line accordingly?
Change your question based on your new question. add how you call the function and where you want it to show so I can change my answer based on that.
Thanks, I added detail, along with a Meteorpad with full example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.