Mongoose 给 node-mongodb-native 驱动程序增加了多少开销?如果我只想对多个集合进行几个结构相似的查询,那么直接使用驱动程序编写所有内容是否更有意义?例如,我需要执行如下操作(使用驱动程序语言,复制自 这里):
db.collection('test', function(err, collection) {
collection.find({'a':1}, function(err, cursor) {
*do something*
}
}
我只需用变量替换 'test'
和 'a'
来保存实际的集合和键我在找。
我觉得在这种情况下直接使用驱动程序编写所有内容是有意义的。有什么理由使用 Mongoose 本身吗?开销是否如此微不足道以至于我考虑直接使用驱动程序是愚蠢的?
最好的,谢谢,
萨米
How much overhead does Mongoose add to the node-mongodb-native driver? If I just wanted to have a couple structurally similar queries on several collections, would it make more sense to just write everything using the driver directly? For example, I would need to do something as follows (in the driver language, as copied from here):
db.collection('test', function(err, collection) {
collection.find({'a':1}, function(err, cursor) {
*do something*
}
}
Where I would just replace 'test'
and 'a'
with variables to hold the actual collection and key that I'm looking for.
I feel like it makes sense in this case to just write everything using the driver directly. Would there be any reason to use Mongoose itself? Is the overhead so negligible that I'm being silly by considering using the driver directly?
Best, and thanks,
Sami
发布评论
评论(2)
取决于您的应用程序有多大。如果您只执行一些查询,那么可能不值得将 Mongoose 添加到其中,但是一旦您的应用程序开始增长,Mongoose 可以帮助保持其更易于维护。开发时间通常比处理时间更有价值。
除非您的应用程序要同时处理大量请求,否则您可能不会注意到性能差异。
Depends how large your app is going to be. If you're only doing a few queries it's probably not worth adding Mongoose to the mix but once your app starts to grow Mongoose can help keep it more maintainable. Dev time is usually more valuable than processing time.
Unless your app is going to be serving a LOT of simultaneous requests you probably won't notice the difference in performance.
使用 Mongoose 的另一个好处是,您可以在应用程序中预先明确定义模型(包括默认值、验证等),这既为您的应用程序提供了大量内在文档,又大大减少了处理时出现不一致的可能性具有相对非结构化的数据模型。
关系数据库具有许多用于定义数据类型、默认值、索引等的功能,如果您(例如)尝试使用不存在的列,则会抛出错误。
Mongo 消除了很多开销,但它创建了更多工作,以确保在实现代码中记录和维护一致的数据模型; Mongoose 让它变得更容易。
它还具有许多有用的功能来控制您的模型,包括使用严格模式的选项(从 v3 开始默认启用),这意味着您尚未定义的任何值都不会写入数据库,'虚拟字段,例如可以将名字和姓氏组合成一个字符串,就好像它存储在数据库中一样(但不一定如此),以及索引管理。有关更多详细信息,请参阅指南。
Another good thing about using Mongoose is that you clearly define your Models (including defaults, validation, etc) up-front in your application, which both provides a lot of intrinsic documentation for your application, and dramatically reduces the possibility of inconsistencies when you deal with a relatively unstructured data model.
Relational databases have a lot of features for defining data types, defaults, indexes, etc. and will throw errors if you (for example) try and use a column that doesn't exist.
Mongo removes a lot of that overhead but it creates more work ensuring that a consistent data model is documented and maintained in the implementation code; Mongoose makes it a lot easier.
It also has a lot of useful features to keep your models in check, including an option to use strict Schemas (on by default as of v3) which means any values you haven't already defined don't get written to the database, 'virtual fields', which for example could combine first name and last name into a string as if it were stored in the database (but it doesn't have to be), and index management. For more details see the guide.