I have a collection that contains two documents: name content
the content contains long texts and what I want to do is I want to search for a specific word in my documents (just inside the content field) and get its name and index.
For example:
id:619c10
name: papers
content: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
id:619c20
name: Stone
content: stie nunc non blandit massa. **Neque** egestas congue quisque egestas diam in arcu cursus. Commodo ullamcorper a lacus vestibulum. Arcu odio ut sem nulla pharetra diam. Commodo viverra maecenas
For example, the word I'm looking for is "Neque" which is in the second document. I want to get this: name Stone Index: 27
I used regex but I don't know what should I do with the response to get name and index.
Subtitle.find({ content: { $regex: 'Neque', $options: 'i' } }, (error, data) => {
if (error) {
console.log(error);
} else {
console.log(data);
}
});
You will have to index the content field to be able to perform search queries, read this: https://docs.mongodb.com/manual/text-search/#:~:text=MongoDB%20supports%20query%20operations%20that,index%20and%20the%20%24text%20operator.&text=Views%20do%20not%20support%20text%20search .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.