I had created a collection in mongo db as show below
db.articles.insert([
{ _id: 1, subject: "one", author: "abc", views: 50 },
{ _id: 2, subject: "lastone", author: "abc", views: 5 },
{ _id: 3, subject: "firstone", author: "abc", views: 90 },
{ _id: 4, subject: "everyone", author: "abc", views: 100 },
{ _id: 5, subject: "allone", author: "efg", views: 100 },
{ _id: 6, subject: "noone", author: "efg", views: 100 },
{ _id: 7, subject: "nothing", author: "abc", views: 100 }])
after that I given text indexing to the field subject and author.
db.articles.createIndex(
{subject: "text",
author: "text"})
Now I am trying to search a word with "one" in indexed field. When I execute query ...
db.articles.count({$text: {$search: "\"one\""}})
... the result is 1
.
The problem is that when I want combination of words "one", "abc" ...
db.articles.count({$text: {$search: "\"one\" \"abc\""}}
... it gives the result as 4
. Including the records that contains the subject name as "lastone", "firstone", "everyone", "one" as the result.
So my question is that why the first query dosn't fetch 4 records? And how can I write a query that can fetch 4 records with word "one"?