Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to store an array of objects in Redis? #209

Open
Ericokim opened this issue Aug 11, 2023 · 1 comment
Open

How to store an array of objects in Redis? #209

Ericokim opened this issue Aug 11, 2023 · 1 comment

Comments

@Ericokim
Copy link

I have an array of Objects that I want to store in Redis. How do i go about it?

@guyroyse
Copy link
Contributor

Redis OM can support this but searching it can be a bit limited as you can only reference the fields within the objects for indexing and they will always be arrays as far as RediSearch is concerned. This means that you can only index string[] and number[] within your object.

Here's a quick example for the following JSON:

{
  "accountNumber": "12345",
  "verified": true,
  "transactions": [
    {
      "approver": "Alice",
      "amount": 12.34,
      "posted": true
    },
    {
      "approver": "Bob",
      "amount": 34.56,
      "posted": false
    }
  ]
}

We can search on all of these fields except posted. We can still save it, we just can't search on it. Here's what the Schema would look like:

const schema = new Schema('account', {
  accountNumber: { type: 'string' },
  verified: { type: 'boolean' },
  approvers: { type: 'string[]', path: '$.transactions[*].approver' },
  amounts: { type: 'number[]', path: '$.transactions[*].amount' }
})

You can then save this JSON and search on these fields with your Repository:

const repository = new Respository(schema, redis)

await respository.save(account) // account contains a JavaScript object as shown above

const accounts = await repository.search()
  .where('accountNumber').equals('12345')
    .and('verified').is.true()
    .and('approvers').contains('Bob')
    .and('amount').is.greaterThan(10.00)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants