0

I find text in mongodb as the same LIKE in MySQL, but the result is not as expected

I have 6 docs in collection posts:

(1) David Beckham's Kids Cheer Him on During Charity Soccer Game

(2) David Beckham is casual in sweater and jeans as he attends Copa

(3) David Beckham spoke out in view of his 55.6 million Instagram followers, saying

(4) David Beckham's family were out in force to support him at the

(5) Handsome: David Beckham looked dapper in a shirt and olive-green jacket before another day of shooting for his eyewear collection in Venice

(6) But on Wednesday, David Beckham was spotted going back to his roots as he caught up with Tottenham ace Harry Kane on the football pitch

SQL's like query:

SELECT * FROM posts WHERE content LIKE '%David%Tottenham%'

=> (6) But on Wednesday, David Beckham was spotted going back to his roots as he caught up with Tottenham ace Harry Kane on the football pitch

Mongo's find query:

db.posts.find({"content": /.*David.Tottenham./})

enter image description here

=> error

I expect the output of Mongo's find query is (6), but the actual output is error

Can Anyone Help Me to find query in mongodb? Thank you!

1 Answers1

2

you can use mongodb $regex operator. I think this query might work for you:

db.posts.find({"content": {$regex: ".*David.*Tottenham .*"}})