0

Thanks in advance. I have a mongoose schema as below:

var bookSchema = new mongoose.Schema({
  name: { type: String, index: true, default: '' },
  text: { type: String, index: true, default: '' },
  date: { type: Date, default: Date.now },
  price: { type: Number, default: 0 } });

bookSchema.index({text: 1, name: 1}, {unique: true});

My problem is when I search for any text in the field named "text" it doesn't work. Possibly a naming conflict, do I have to change the field name to something other than text...

Book.find( { $text : { $search : 'mongoose is great' } } )...
Mendo
  • 319
  • 2
  • 7
  • what error are you getting, how to you define model? – Rakesh Soni May 10 '17 at 02:58
  • One big problem I identified is that I needed to restart the MongoDB server... (doh!). And I wrote a short function bookSchema.pre('save', function(next) { this.searchable = this.text + ' ' + this.name; next(); }); To include all that is searchable in a new field. – Mendo May 10 '17 at 03:38
  • @Mendo You don't need to restart MongoDB when adding an index, you don't need to create a separate field for searching with a text index, and the answer you marked correct isn't right either. Bad information overload! You should probably take a fresh run at this. – JohnnyHK May 10 '17 at 05:00
  • Possible dupe of http://stackoverflow.com/questions/28775051/best-way-to-perform-a-full-text-search-in-mongodb-and-mongoose – JohnnyHK May 10 '17 at 05:26
  • You might be right... I am using mLabs. I spun up a new mongo server and things are great, as for the old mongo server I still can't achieve the desired indexing so I am struggling for answers. – Mendo May 11 '17 at 05:55

2 Answers2

1

text is a reserved word in MongoDB. Don't use this word, try with another.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Chen
  • 9
  • 1
  • 3
0

Thanks for your effort in helping.

The issue I had was a small but painful lesson. As it turns out I went into my mLabs console and noticed that the Indexes for the collection totaled more than 16! So deleted all of them and added the one I needed. It turned out great, fixing my problem.

So I am guessing that if you keep adding Indexes without deleting the prior one's you end up getting unpredictable results.

Thanks again to all that answered.

Mendo
  • 319
  • 2
  • 7