Answer the question
In order to leave comments, you need to log in
How to implement hashtag search in mongodb?
Good morning comrade friends.
There are a bunch of documents in Mongo, I want to attach hashtags to them so that they can be searched. The question is how to do it as quickly as possible in terms of performance?
{
date: '2015-09-21 22:12:25',
author: 'alex',
content: 'Юпитер - планета в сол...',
tags: ["планеты", "юпитер", "небо"]
}
Answer the question
In order to leave comments, you need to log in
Your structure has the right to life.
To search quickly, create an index on the tags field
Search on an array field:
//простой
collection.find({ tags : 'планеты' })
//или планеты или юпитер
collection.find({ tags : { $in : ['планеты', 'юпитер'] } })
//и планеты и юпитер
collection.find({ tags : { $all : ['планеты', 'юпитер'] } })
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question