K
K
Khurshed Abdujalil2017-04-18 11:31:00
MongoDB
Khurshed Abdujalil, 2017-04-18 11:31:00

How to do full text search in mongodb on multiple fields?

there is a table let's say products , fields id, name, descrption...
created an index

db.products.createIndex({'name': 'text', 'description': 'text'});

so that he searches both by the title and the text, but for some reason he searches by description only
db.products.find({$text : {$search : 'Мебель'}})
If there is furniture in the name but not in the description, then he skips this entry, and if on the contrary, he finds what is wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Khurshed Abdujalil, 2017-04-19
@akhur

I don’t know how to solve the search for two fields, I made one field and added the name and description values ​​​​then I created an index, and already the search for a new field

E
emp1re, 2017-04-18
@emp1re

db.products.aggregate([
            { $match: {
                $or: [
                    { 'name': { '$regex': req.query, '$options': 'i' } }, 
                    { 'description': { '$regex': req.query, '$options': 'i' } }
                ]
            } }])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question