javascript - Find the last item in array in mongo document and check if its field is a certain value -


i got document:

{ _id: "zaphzeqw98uhwqaey", borrowerid: "dmgqyqenbnt4ebmia", isseenbyother: 1, lenderid: "jsjyvseqiiazgxruq", messages: [{    date: sun oct 25 2015 19:40:25 gmt+0100 (cet),    from: "jsjyvseqiiazgxruq",    text: "hi there" },{    date: sun oct 25 2015 19:40:35 gmt+0100 (cet),    from: "dmgqyqenbnt4ebmia",    text: "hey!" }] } 

what i'm trying boolean value stating whether or not value of field from of the last object item in array: messages current user.

i have tried lot of different mongodb projections such $slice , $position inserts in beginning of array not ideal case.

you can use underscore's _.last() function follows:

var doc = mycollection.findone({ _id: "zaphzeqw98uhwqaey" }); var lastelement = _.last(doc.messages); if ( lastelement.from === meteor.userid() ){   ... thing } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -