-1

I'm using mongodb 4.4 and making aggregate query to find data from the collection.

Here is my code:

db.getCollection('users').aggregate([
    {
        $match: {
            $or: [{
                "name": {
                    $regex: /a/,
                    $options: 'i'
                },
                "email": {
                    $regex: /a/,
                    $options: 'i'
                }
            }]
        }
    }
])

I want to do query just like SQL like on all column of the user collection. I am not getting any result with this.

CASE 1: If I use /(:?)/ it does return me all the collection and that works fine CASE 2: If i user /ad/ it does not return anything even though there is email and name containing admin string.

Mysterious Coder
  • 148
  • 2
  • 4
  • 20

1 Answers1

-1

Finally found the issue after getting too much over the docs.

there is was a slight problem with the syntax of $or

what worked for me is something like this:

db.getCollection('users').aggregate([
    {
        $match: {
                    $or: [
                        {
                            "name": {
                                $regex: /a/,
                                $options: 'i'
                            }
                        },
                        {
                            "email": {
                                $regex: /a/,
                                $options: 'i'
                            }
                        }
                    ]
                }
    }
])
Mysterious Coder
  • 148
  • 2
  • 4
  • 20