M
M
My joy2016-02-26 05:12:25
MongoDB
My joy, 2016-02-26 05:12:25

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: ["планеты", "юпитер", "небо"]
}

So? If so, can you tell me how to search for them?
Or do you need to make a separate database with only tags, and give them DOCID?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-02-26
@t-alexashka

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 question

Ask a Question

731 491 924 answers to any question