1

I am trying to query a collection with a like query, which we do in relational DB. I am trying to get a config which ends with LOYALTY_SERVICE_ENABLED

However, I couldn't use like query as I wanted. Can you explain the difference between the two queries below? Thanks

RETURNS 1 RESULT

db.getCollection("configurations").find(
    { key: 'co:food:LOYALTY_SERVICE_ENABLED' }
);

RETURNS 0 RESULT

db.getCollection("configurations").find(
    { key: '/.*LOYALTY_SERVICE_ENABLED' }
);

Equal SQL Query:Select * from configurations where key like '%LOYALTY_SERVICE_ENABLED';

helcode
  • 1,859
  • 1
  • 13
  • 32
Berkin
  • 1,565
  • 5
  • 22
  • 48

2 Answers2

2

It looks like a typo. can you try it like this?

db.getCollection(“configurations”).find({"key" : /.*LOYALTY_SERVICE_ENABLED.*/}).pretty();
J.F.
  • 13,927
  • 9
  • 27
  • 65
1

You will need to use the $regex evaluation query operator to use a pattern matching string, I have modified your code to reflect that.

db.getCollection("configurations").find(
    { key: {$regex: /.*LOYALTY_SERVICE_ENABLED/} }
);

Notice you don't need the quotes ' ' around the pattern string.

More details can be found here

helcode
  • 1,859
  • 1
  • 13
  • 32
  • thanks for reply. Main problem was using ' ' inside of query. When I omit that as above answer, it fixed. Thanks – Berkin Nov 24 '20 at 08:20