0

I am doing a find() operation on my collection like this:

db.posts.find({
    "$or":[
        { "name": keywords },
        { "description": keywords },
        { "tags": keywords }
    ]
});

keywords is a RegEx, everything works great.

But I was wondering if there was a way in MongoDB to know which field matched name or description or tags. Or a way to count matching fields.

Thank you.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
picsoung
  • 6,314
  • 1
  • 18
  • 35
  • If you are relying on the count of matching fields to sort result, I think text index in mongo 2.6 is a better choice. http://docs.mongodb.org/manual/core/index-text/ – yaoxing May 08 '14 at 01:38
  • thans for your comment. if I can add more details: on one result that matches the result, I am looking to know if it matched on `name`,`description` or `tags`. I want to do it on an individual level on each result to help me sort them by "accuracy" Did not see anything like this on index-text. – picsoung May 08 '14 at 01:51
  • text index is more like a simple search engine. you are able to control the **weight** of each field, then control the sort order. Have a look at [this section](http://docs.mongodb.org/manual/tutorial/control-results-of-text-search/) – yaoxing May 08 '14 at 01:58
  • I should have mentioned that I am using Meteor with minimongo and it seems that text search is not implemented. :( – picsoung May 08 '14 at 17:17

0 Answers0