NilmeX
NilmeX

Reputation: 63

RethinkDB filter not finding results

I have an issue with RethinkDB (NodeJS).

I'm trying to use a simple .filter() function but for some unknown reason it doesn't want to find a result. Currently my code looks like this:

     const someID = 1234;

     r.table('list').filter({id: someID}).run().then((err, result) =>{
      if(err) throw err;
      console.log(result)
     })

I know that the result with ID '1234' is there because if I run this query:

     const someID = 1234;

     r.table('list').filter({id: 1234}).run().then((err, result) =>{
      if(err) throw err;
      console.log(result)
     })


It works just fine.

the only change I made is give the ID directly instead of making it a variable/constant.

What might be the issue?

Upvotes: 1

Views: 184

Answers (2)

NilmeX
NilmeX

Reputation: 63

For anyone having the same issue, I've resolved it by changing my object in the database. I've deleted my id field so Rethink Generates it's own key, and now I'm using that as my object id. It works just fine.

Upvotes: 0

Ben Steward
Ben Steward

Reputation: 2408

If you are searching by the primary index of a table (usually id), then get is probably the tool you are looking for:

const itemId = 1234;

r.table('list').get(itemId)

If you are looking for something based on a secondary key, then getAll will serve your purposes:

  1. Create the key, if necessary:

    r.table('list').indexCreate('val')
    
  2. Access the data:

    const itemVal = 'hey'
    
    r.table('list').getAll(itemVal, {index: 'val'})
    
    // would return data like: { id: 1234, val: 'hey' }
    

It appears that the first use case is what you're looking for, but I thought you might find the second helpful.

Upvotes: 0

Related Questions