简体   繁体   中英

How can I search for specific words in mongodb using mongoose

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);
  }
});

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM