1

So I'm using Node.js with MongoDB for my web application. I'm having some trouble creating a text index for my schema and searching for text within an array. I've looked at the mongo docs but haven't found anything related to this specifically.

My current implementation searches successfully on regular String values, but querying for text matching in [String]'s don't return anything.

Here's my REST call:

...console.log("Query string: " + str); var qry = { "$text": { "$search": str } }; model.find(qry, function (err, results) {...

And when I create my schema:

var blah = new Schema({ foo : String, bar : [String],

...

blah.index({ foo: 'text', bar: 'text' });

Any query won't return the results that match in bar. A query string for something within foo works fine.

1
  • 1
    Please post the statement you've used to create your text index. This is relevant to the question. Commented Apr 2, 2015 at 7:43

2 Answers 2

1

Double check that you've created the correct indexes on the correct collections and the queries are being issued to the correct collections. Indexing an array works for me:

> db.test.drop()
> db.test.insert({ "_id" : 0, "a" : "dogs are good" })
> db.test.insert({ "_id" : 1, "a" : "I like dogs", "b" : ["where's my dog?", "here, have a cat"] })
> db.test.insert({ "_id" : 2, "b" : ["she borrowed my dog", "my frogs are croaking"] })
> db.test.ensureIndex({ "a" : "text", "b" : "text" })
> db.test.find({ "$text" : { "$search" : "dogs" } }, { "_id" : 1 })
{ "_id" : 0 }
{ "_id" : 2 }
{ "_id" : 1 }
Sign up to request clarification or add additional context in comments.

Comments

0

Okay, I finally figured it out! Turns out, grunt serve doesn't update indexes in the database. I had created a text index for "foo" only and that didn't update when I added "bar" to the index. I had to run - in mongo shell:

db.dropDatabase()

The next time I ran it, the database was recreated and the proper indexes were set. If anyone else runs across this issue, try running db.getIndexes().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.