0

I ma returning journals back from Mongo using node , can I iterate over the response object (array or journals) and then add properties on them.

    for (i=0; i < results.lenth ; i++){

        if (results[i].viewedThi.index of (mee)){

            result.newProperty = "seen this!";

        }

}

Excusing my code syntax I was wondering if I may do this or is there a more elegant way. I want node to look through each journals list of viewers and then add a "viewed this" property to each one before returning it to the client. I want to use this new property for my client's templating

1)QUERY MONGO 2)Get objects that represent enity 3) look through array property of object for value 4)if value found add a property to denot that value has been found

ex: Mongoose returned

book =[
{title:"bird book1", readers ['james', 'nick'] }
{title:"bird book2", readers ['max', 'kevin'] }
{title:"bird book3", readers ['blake', 'rick', 'manny'] }
]

I want to look through the books and then turn them to

 book =[
    {title:"bird book1", iReadThis: true},
    {title:"bird book2", iReadThis: false},
{title:"bird book3",     iReadThis: true}

    ]

My intent is to add a property before returning the journals to the user. The property identifies a piece of context pertinent to the user.

user3461101
  • 45
  • 1
  • 6
  • Your question is not clear. What is it you are trying to do ? What are results ? Journals ? What does if (results[i].viewedThi.index of (mee)) mean ? What is mee ? You really should improve your writing. – Leonardo May 10 '14 at 06:35
  • I am querying mongo using mongoose and then getting an array of objects. Can I add porperties on each object. – user3461101 May 11 '14 at 02:00
  • So actually, what you are trying to achieve is a 'Viewed' tag right ? Try seeing this question http://stackoverflow.com/questions/18573117/updating-nested-arrays-in-mongodb-via-mongo-shell. – Leonardo May 11 '14 at 02:43
  • I am just trying to add a property to each of the result objects in in the results array from the query. – user3461101 May 14 '14 at 14:40
  • Ok, it's clear now. Have you tried the link that I posted above ? – Leonardo May 14 '14 at 17:27

0 Answers0