'MongoDB text search doesn't search single word
so I want to search through my blogs collection in my MongoDB database. I want to search by title, I create the index with mongoose in the schema's file. Everything is cool but if I for example have a blog with a title like this: "averylongtitlewithoutspaces", it won't search it unless I type in the whole title. It's works okay with spaces so searching "a very" returns "a very long title with spaces". How can I make mongo search the text with spaces?.
Here's my schema file
import { Schema, model } from "mongoose";
interface IBlog {
title: String,
content: String,
author: String,
authorId: Schema.Types.ObjectId,
shortContent: String,
image: String
}
const BlogSchema = new Schema<IBlog>({
title: {
required: true,
type: String
},
content: {
required: true,
type: String
},
author: {
required: true,
type: String
},
authorId: {
required: true,
type: Schema.Types.ObjectId,
ref: "Account"
},
shortContent: {
required: true,
type: String
},
image: {
requred: true,
type: String
}
}, {
timestamps: true
});
BlogSchema.index({ title: "text" }, {default_language: "none"});
export const Blog = model("Blog", BlogSchema, "blogs");
Query that I'm making:
const blogs = await Blog.find({ $text: { $search: req.params.input } });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
