Iano
Iano

Reputation: 30

MongoDb LIKE on two fields combined with AND

I'm looking for MySQL's equivalent of

SELECT * FROM table WHERE field1 LIKE '%somestring%' AND field2 LIKE '%anotherstring%'

I did try a couple of things, including

'field1' => '/somestring/i', 'field2' => '/anotherstring/i'

I'm getting no results back, or just data based upon one of the two fields. Is this not possible in MongoDB?

Upvotes: 1

Views: 325

Answers (1)

Sushant Gupta
Sushant Gupta

Reputation: 9458

 db.collection.find({field1: {$regex: /somestring/i}, field2: {$regex: /anotherstring/i}});

In php you should do by MongoRegex.

array(field1 => new MongoRegex("/somestring/i"), field2 => new MongoRegex("/anotherstring/i"))

Upvotes: 2

Related Questions