简体   繁体   中英

Custom mongodb search object?

I'm building my first node.js/angular app and cannot seem to find a solution for the following problem.

If I have a User Schema such as :

User = {
  firstname: "Bryan",
  lastname: "Allan",
  email: "noemail@example.com,
  skills: ["node.js", "angular", ..]
  ...
}

Is it possible to both:

a. Search on one or multiple fields?
b. Search for the instance of string?

For example, can I use a custom search object like:

search = {
  firstname: "Br",
  skills: "node.js"
}

and have it return the above document? Is a search object the best solution?

Normally, I would return all Users and use an angular filter, but I'm worried that this approach won't scale nicely since there's going to be a large amount of users stored. With this in mind, I'd like to limit the results sent back to the browser if possible.

If an array just contains simple values (as in your example) you can just query it with

User.find({ firstname: "Br", skills: "node.js })

The mongoose method .find() is very powerfull. https://docs.mongodb.org/manual/reference/method/db.collection.find/

Or you could also use the aggregate() method https://docs.mongodb.org/manual/reference/method/db.collection.aggregate/

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