Search code examples
node.jsmongodbsearch-enginedatabase

Best database/solution for searching if any of db.item.tags is contained in given string


Title might be confusing - let me explain:

I have to store data in format like this (this example is in mongoDB format):

{
    name: 'entity1',
    field: 'value',
    tags: ['tag1','tag2','tag3'],
}

Now I need function like this:

function(text){
    // find all entities where text contains at least one of entity tags
}
  1. Usually number of tags should not be greater than 5.
  2. Number of entities should not be greater than 100 but it may grow in the future.
  3. I need this in "realtime" - so as fast as possible.
  4. Search has to be case-Insensitive.
  5. Data have to be stored in some kind of DB - when db is updated then function searches through updated entities.
  6. My app is written in node.js.
  7. It would be cool if function returned information on what position of string each tag was found (if it was found at all).

I tried to accomplish this on mongoDB and found solution to use $where query but it does mot work on array attribute and anyway it is pretty slow.

I am looking for solution/db I could use...


Solution

  • You are getting downvotes as your question does not relate to any code-related problem, but a generic educational question based on lack of knowledge. SO does not encourages such type of questions. As you basically asking others to do your homework.

    Answer:

    MongoDB allows to create index for strings in array:

    db.item.ensureIndex({ tags: 1 });
    

    And find just by:

    db.item.find({ tags: 'tag2' });
    

    MongoDB is smart enough to understand that it is array and you are looking for value in array.