0

I have a field in a MongoDB Collection like:
{ place = ['London','Paris','New York'] } I need a query that will return only that particular entry of the array, where a specific character occurs. For Example, I want to search for the terms having the letter 'o'(case-insensitive) in them. It should just return 'London' and 'New York'.
I tried db.cities.find({"place":/o/i}), but it returns the whole array.

2 Answers 2

2

You'll need to $unwind using an aggregate query, then match.

db.cities.aggregate([ { $unwind:'$place' }, { $match: { place : {$regex: /o/i } } } ])
Sign up to request clarification or add additional context in comments.

Comments

-1

In simple you find with $regex below query will work without aggregation

db.collectionName.find({ place : {$regex: /o/i } })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.