简体   繁体   English

自定义mongodb搜索对象?

[英]Custom mongodb search object?

I'm building my first node.js/angular app and cannot seem to find a solution for the following problem. 我正在构建我的第一个node.js / angular应用程序,似乎无法找到以下问题的解决方案。

If I have a User Schema such as : 如果我有一个User架构,例如:

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. 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. 通常,我会返回所有Users并使用角度滤镜,但是我担心这种方法无法很好地扩展,因为会存储大量用户。 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. 猫鼬方法.find()非常强大。 https://docs.mongodb.org/manual/reference/method/db.collection.find/ 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/ 或者,您也可以使用Aggregate()方法https://docs.mongodb.org/manual/reference/method/db.collection.aggregate/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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