Sagar T
Sagar T

Reputation: 89

How to add multiple text search in mongo full text search using pymonog

I have monogo collection with column name server_name an dos_name like below.

enter image description here

i have added index on server_name

db.createIndex({"server_name":"text"})

I want to search the record in with server name "Apple Mac pro" and "Cisco USB Mini"

I have tried below query.

db.collection_name.find({"$text": {"$search": '"Apple Mac Pro" "Cisco UCS Mini"'}});

but it giving blank result.

I have tried different different approaches like and multiple text search but want work.

thank you for your help.

Upvotes: 2

Views: 51

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

Specifically for what you're describing you should just use $in with an exact match:

db.collection_name.find({ server_name: { $in: ['Apple Mac Pro', 'Cisco UCS Mini'] } });

Assuming you do really want a $text search then you have to execute two separate calls, due to the $text restrictions

A query can specify, at most, one $text expression.

If the $search string includes a phrase and individual terms, text search will only match the documents that include the phrase.

Essentially two phrases execute $and logic between them, and because you can only execute a single $text query you need to split it into 2 calls.

Upvotes: 2

Related Questions