So I was reading Kyle’s book and came across this example he has used for using regular expressions to match some text:
db.reviews.find({
user_id: ObjectId("4c4b1476238d3b4dd5000001"),
text: /best|worst/i
})
But it doesn’t have a sample document so I can’t imagine how a document is structured that he is using for this example. So can you please draw a quick document that the regular expression above works on it?
As far as I can tell it has a _id key of course and a text key, but what the values of that text key look like?
Thanks.
Imagine something like this, there is a user collection elsewhere, and this is a collection of all reviews every user ever wrote. When you want to get all reviews by a particular user, you query for all reviews which match that user’s id – that’s the user_id part. Now you decide you only want the reviews that have the word best or the word worst in it (regardless of capitalization). That’s the second part of the query.
Assuming the three documents below all had matching user_id then first and third would match the text query but the middle one would not.