# Trying to read last 100 entries from DynamoDB in AWS

**URL:** <https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801>\
**Category:** General\
**Created:** [10 March 2019 11:37 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801 "2019-03-10T11:37:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [10 March 2019 11:37 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/1 "2019-03-10T11:37:54Z")

</div>

Hello

I have some entries in a DynamoDB on AWS which contain my IoT Device ID, the time of the entry and a payload with all the data my IoT device measured.

I am wanting to extract this data from the DB to input it into a UI chart.

The only examples I can find on the internet are regarding pushing data to the DB or extracting a single entry.

Can anyone provide any examples or guidance?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 March 2019 11:59 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/2 "2019-03-10T11:59:24Z")

</div>

Did you find the link below? I haven't use DynamoDB but it appears you can most things that you can do with sql databases. If you want just the samples after a given time you can use that as a query condition, or if you want just the last 100 records then tell it to sort in reverse order and then use limit to fetch only the first 100 (which are actually the last 100). They will be in reverse order but you can re-order after you get them, or perhaps you can get the database driver to sort them again after fetching them.  
[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html)

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [10 March 2019 12:02 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/3 "2019-03-10T12:02:17Z")

</div>

Thanks for the reply.

Will the node-red-contrib-dynamodb-scan node do the trick?

It is expecting the following input:

```auto
{
    key: "nme",
    reservedKey: "name",
    value: true,
    operator: "="
}

```

What do these paramaters represent?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 March 2019 12:07 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/4 "2019-03-10T12:07:37Z")

</div>

> [@Sirhc](#):
>
> Will the node-red-contrib-dynamodb-scan node do the trick?

Sorry, no idea. As I said I haven't used DynamoDB. Isn't there a node that allows you to do generic queries on the db?

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [10 March 2019 12:09 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/5 "2019-03-10T12:09:55Z")

</div>

It would be absolutely beyond me to try and write a node from scratch to access DynamoDB. It appears there are plenty of nodes out there that do it, there's just very little in the way of documentation on how to utilise them.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 March 2019 12:12 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/6 "2019-03-10T12:12:25Z")

</div>

I wasn't suggesting writing a node that allows generic queries, I was suggesting looking for one that does and then using it.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [10 March 2019 12:13 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/7 "2019-03-10T12:13:32Z")

</div>

So how would yo do it without using Node-RED? If you can post your search query here hopefully someone would be able to translate that for you.

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [10 March 2019 13:14 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/8 "2019-03-10T13:14:15Z")

</div>

OK So I've made some progress after hours of reading. I am able to use the AWS DynamoDB node with scan paramater from node-red-contrib-aws.

I now return content from my DynamoDB with each entry stored into an array. I have written a function to try an extract individual entries from the array and store them into another array to plot on a UI chart.

My code is as follows (to extract time and temperature). But I see nothing in the debug node after the function. Am I doing something wrong?

 ![query](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4c0149801db01d7f19f96bf9d32574d6f45f0b33.png)

```auto
var x=0;
var myArray = {};
var i=0;

for (i=0; i< msg.payload.ScannedCount; i++) {

myArray[x] = {
time: (msg.payload.Items[i].time),
temperature: (msg.payload.Items[i].payload.M.Temperature)
}
x=x+1
}

msg.payload = myArray;
return msg;

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [10 March 2019 13:30 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/9 "2019-03-10T13:30:17Z")

</div>

You can add debug lines to your function by using node.warn commands see [https://nodered.org/docs/writing-functions#logging-events](https://nodered.org/docs/writing-functions#logging-events)

You can also check the path to the data item using the tool tips as described here: [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [10 March 2019 13:36 UTC](https://discourse.nodered.org/t/trying-to-read-last-100-entries-from-dynamodb-in-aws/8801/10 "2019-03-10T13:36:02Z")

</div>

Had to connect both lines to the function! argh!
