Database Reference
In-Depth Information
"body" : "i like fish",
"about" : "food"
}
}
],
"stats" : {
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 1,
"nfound" : 1,
"timeMicros" : 101
},
"ok" : 1
}
That's perfect; we've returned only the one item as we wanted, without getting the unrelated “fishing” document.
Notice that the
nScanned
and
nscannedObjects
values are 2, which denotes that this query scanned two documents
from the index (
nScanned
) and then had to retrieve these two documents to review their contents (
nScannedObjects
)
to return one matching document (
n
). Now let's look at another example.
More Involved Text Searching
First run the following query, which will return two documents. The results have been cut down to just the text fields
for brevity.
db.texttest.runCommand( "text", { search : "cook" })
"body" : "i want to cook dinner",
"body" : "i am to cooking lunch",
As you can see, we have two documents, both of which are about cooking a meal. Let's say we want to exclude
lunch from our search and only return dinner. We can do this by adding
-lunch
to exclude the text
lunch
from
our search.
> db.texttest.runCommand( "text", { search : "cook -lunch" })
{
"queryDebugString" : "cook||lunch||||",
"language" : "english",
"results" : [
{
"score" : 0.6666666666666666,
"obj" : {
"_id" : ObjectId("51d7ccb36bc6f959debe5518"),
"number" : 5,
"body" : "i want to cook dinner",
"about" : "activities"
}
}
],
"stats" : {
"nscanned" : 2,
"nscannedObjects" : 0,