MongoDB - Returning the most recent document (Or just a single document in general)

418 views
Skip to first unread message

Andrew Doodson

unread,
Jul 17, 2017, 5:44:35 AM7/17/17
to Node-RED
Hi All, 

I've been going a little nuts trying to figure out where I'm going wrong here and I'm guessing there's some rookie error going on.

What I want to do: Return the most recent entry or ultimately number of entries from a MongoDB collection.

How I'm attempting but failing to do this at the moment: 

Node 1 - Injection node containing db.find()

Node 2 - MongoDB node with the correct Server, Collection and find operation - This seems to return the whole collection - I don't want the whole thing, it's ginormous.

Node 3 - A function node - I know I need this, I don't know what to put in it - I've been playing around with no joy.

I was able to successfully return one single document object by hard coding the index of the msg.payload array with the following in the function node...

var msgs = [];
msgs
.push({payload: msg.payload[33]});
return [msgs];

but any attempt to get the array length and use it as the index (or similar)  has been futile ...

var msgs = [];
var data = [{payload: msg.payload.length}]; // Get the length, this seems to return a number
var foo = parseInt(data[0]); // I'm lost from this point on...
var bar = foo - 1; // I'm guessing I need th emax number - 1 to get the end of the array??
msgs
.push({payload: msg.payload[bar]});
return [msgs];// Nope! Returns: Undefined

Am I getting close?

Node 4 - Debug - Continually shows how I fail.

Any help, further reading or general kick in the butt would be appreciated,

Many thanks,

Andrew

steve rickus

unread,
Jul 17, 2017, 10:05:41 AM7/17/17
to Node-RED
Hi Andrew,

Sounds like you just need a good primer on how to access javascript array elements -- I like the one at the W3Schools web site... This shows how to get the array length and use the index number to select only the element you want (you are right, you need to subtract 1 since arrays are 0-based). There are also built-in functions like pop() and shift() for extracting elements at the beginning and end of an array.

However, returning the entire (ginormous) database and then only using one element is a bad idea! Instead, there should be a way to return only the element you need from mongodb -- but I've not used it, so I can't help you there.
--
Steve

Nick O'Leary

unread,
Jul 17, 2017, 10:36:38 AM7/17/17
to Node-RED Mailing List
Hi Andrew, which mongodb node are you using? There are a couple in the community.

Our one (node-red-node-mongodb) allows you to pass in msg.sort and msg.limit to the node in order to modify the query.

If your entries can be sorted by an field called id, then the following Function will set the appropriate properties on the message that, when passed to the mongo node, will return the 5 most recent entries.

msg.sort = {"id":-1};
msg.limit = 5;
return msg; 


Regards,
Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/c24cc26e-48e8-4260-a5af-6c627784b26f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Doodson

unread,
Jul 17, 2017, 10:47:24 AM7/17/17
to Node-RED
Thanks so much Steve and Nick! I will definitely read up on the JavaScript deets on W3Schools.

@Nick, I am using your node yes, I'll give this method a go, all my functions are AFTER the mongo node so I'm seeing my mistake now where the query into the mongo node should be limiting the results not the function after the mongo node sifting through all the results, makes complete sense, will post successful flow if I get there for future hacks like myself.

You've certainly saved me a few strands of hair.

Reply all
Reply to author
Forward
0 new messages