0

I want to search(query) a bunch of strings from a column in DynamoDB. Using Dynamoose https://github.com/dynamoose/dynamoose But it returns nothing. Can you help if this type of query is allowed or is there another syntax for the same.

Code sample

Cat.query({"breed": {"contains": "Terrier","contains": "husky","contains": "wolf"}}).exec()

I want all these breeds , so these are OR queries. Please help.

jolly
  • 3,353
  • 1
  • 15
  • 21

1 Answers1

5

Two major things here.

First. Query in DynamoDB requires that you search for where a given hasKey that is equal to something. This must be either the hashKey of the table or hashKey of an index. So even if you could get this working, the query will fail. Since you can't do multiple equals for that thing. It must be hashKey = _______. No or statements or anything for that first condition or search.

Second. Just to answer your question. It seems like what you are looking for is the condition.in function. Basically this would change your code to look like something like:

Cat.query("breed").in(["Terrier", "husky", "wolf"]).exec()

Of course. The code above will not work due to the first point.

If you really want to brute force this to work. You can use Model.scan. So basically changing query to scan` in the syntax. However, scan operations are extremely heavy on the DB at scale. It looks through every document/item before applying the filter, then returning it to you. So you get no optimization that you would normally get. If you only have a handful or couple of documents/items in your table, it might be worth it to take the performance hit. In other cases like exporting or backing up the data it also makes sense. But if you are able to avoid scan operations, I would. Might require some rethinking of your DB structure tho.

Cat.scan("breed").in(["Terrier", "husky", "wolf"]).exec()

So the code above would work and I think is what you are asking for, but keep in mind the performance & cost hit you are taking here.

Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
  • Thanks for great clarification. It clarifies, but of course I won't deploy it for performance. – jolly May 19 '20 at 22:19