2

I am performing a search on the collection based on different columns. Basically what I need is that when I search, the name starting with the keyword entered should appear on top of the list, and then other names from different columns that contain the keyword should appear.

I am having a hard time finding direct functionality in MongoDB to perform this operation. Any logic is appreciated.

example :

[
  { name : Pro1},
  { name : testpro},
  { name : Pro2},
  { name : testpro2}
]


so when I query on this array with the keyword **Pro** the expected output should be :

[ Pro1,Pro2,testpro,testpro2]


so that all the names starting with the exact keyword should appear first then the list of names containing the keyword should be there.
2
  • 1
    give an example if you can, with sample data and expected output, in valid json text, looks like you need regex match but i am not sure Commented Aug 10, 2022 at 17:38
  • added an edit including the example, I am already using regex but I am confused as to how to maintain two queries in an optimized way or something. !! @Takis Commented Aug 11, 2022 at 6:49

0

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.