1

I work on an online search project. Its database is MongoDB I want to make a search textbox that works like: if I want to search for a book
The author is "Joe" The title is "my book" and publisher is "tia"

I want to type in the search textbox: "joe my book tia" or "tia jo book"

so I will get a result for it.

Author, Title, and publisher are in the JSON on the MongoDB

can Some Body Help me?

This is a Sample about JSON:

{ "Author": [ { "id": "1", "name": "Joe", "last name": "Jeff" }, { "id": "1", "name": "Lama", "last name": "moon" } ], "Title": "My Book", "Publisher": "tia" }

Ali.K
  • 45
  • 1
  • 7

1 Answers1

1

MongoDB provides text indexes to support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements.

In your case you can run the following in a mongo shell to allow text search over the Author name and Publisher fields:

db.test.createIndex({ 'Author.name': 'text', 'Author.last_name': 'text', Publisher: 'text'})

Then you could use the following query to find all documents containing any terms from the list "joe", "my", "book" and "tia".

db.test.find({ $text: { $search: "joe my book tia" } })
Jitendra
  • 3,135
  • 2
  • 26
  • 42
  • Okay that what I want but also I need to put on the search for the name "Sara" "Sar" the search will find each indexed name with "Sar*" – Ali.K Feb 15 '19 at 10:08
  • It's not posible to do it with $text operator. Text indexes are created with the terms included in the string value or in a strings array and the search is based in those idexes. You can only group terms on a pharse but not take part of them. for reference please read this post : https://stackoverflow.com/questions/24343156/mongodb-prefix-wildcard-fulltext-search-text-find-part-with-search-string – Jitendra Feb 15 '19 at 10:28