如何使用 mongoose 驱动程序提高 MongoDB 性能?
How to improve MongoDB performance with mongoose driver?
我在 MongoDB 中有 2 个集合。
- 文档
- 数据
Document
有大约 5000 个文档,获取总数非常快。
但后来我又添加了一个集合 Data
,其中有 1M 条记录。现在,如果我尝试获取 Document
中的记录总数,它会在 Mongoose 中花费 20 秒。但在 Mongo 控制台中速度更快。
//文档架构
var docSchema = mongoose.Schema({
docId: String,
articleid: String,
title:String,
lastModified: String,
});
docSchema.index({ docId: 1, articleid: 1 ,lastModified:1});
var Doc = mongoose.model('document', docSchema);
module.exports = Doc;
//数据模式
var allData = mongoose.Schema({
_id:String,
id: Number,
author:String,
});
allData.index({ id: 1});
var data = mongoose.model('Data', allMetricsData);
module.exports = data;
Node.JS 中的聚合函数如下:
Document.count(function(errs, count) {
console.log("Count is:",count);
});
那么如何使用 Mongoose 提高性能?
当我执行时,db.documents.count()
我立即得到响应。
首先,不要忘记在查询中使用的所有主要字段上放置索引。
例如,如果您的所有查询都使用 'name' :
Collection.find({name: "aaa"})
或'name'和'age':
Collection.find({name:"aaa", "age": 4})
将索引放入您的模型中,如下所示:
var mongoose = require("mongoose")
ActionCode = new mongoose.Schema({
name: {type: String, default: ""},
age: {type:Number, default: 0},
otherfield: {type:Number, default: 0},
});
ActionCode.index({name:1});
ActionCode.index({name:1, age: 1});
module.exports = mongoose.model('ActionCode', ActionCode);
您的索引将被推送到内存中,据此可以使文件的分辨率更快。
https://docs.mongodb.com/manual/indexes/
然后,由于 returned 文档的数量,它可能需要一些时间,只有 select 需要的字段,像这样 mongo 将只 return 到您的服务器需要字段而不是整个文档:
例如,如果您只需要姓名、年龄和类型字段:
Collection.find({name: "aaa"},{name:1, age:1, field:1}, function(err, response){
// returned documents here contains only following fields: _id, name, age, field
=======
尝试
Collection.find({}).select({_id: 1}).count(function(err, result)
或尝试聚合:
Collection.aggregate([{$group:{_id: null, count:{$sum :1}}])
我在 MongoDB 中有 2 个集合。
- 文档
- 数据
Document
有大约 5000 个文档,获取总数非常快。
但后来我又添加了一个集合 Data
,其中有 1M 条记录。现在,如果我尝试获取 Document
中的记录总数,它会在 Mongoose 中花费 20 秒。但在 Mongo 控制台中速度更快。
//文档架构
var docSchema = mongoose.Schema({
docId: String,
articleid: String,
title:String,
lastModified: String,
});
docSchema.index({ docId: 1, articleid: 1 ,lastModified:1});
var Doc = mongoose.model('document', docSchema);
module.exports = Doc;
//数据模式
var allData = mongoose.Schema({
_id:String,
id: Number,
author:String,
});
allData.index({ id: 1});
var data = mongoose.model('Data', allMetricsData);
module.exports = data;
Node.JS 中的聚合函数如下:
Document.count(function(errs, count) {
console.log("Count is:",count);
});
那么如何使用 Mongoose 提高性能?
当我执行时,db.documents.count()
我立即得到响应。
首先,不要忘记在查询中使用的所有主要字段上放置索引。
例如,如果您的所有查询都使用 'name' :
Collection.find({name: "aaa"})
或'name'和'age':
Collection.find({name:"aaa", "age": 4})
将索引放入您的模型中,如下所示:
var mongoose = require("mongoose")
ActionCode = new mongoose.Schema({
name: {type: String, default: ""},
age: {type:Number, default: 0},
otherfield: {type:Number, default: 0},
});
ActionCode.index({name:1});
ActionCode.index({name:1, age: 1});
module.exports = mongoose.model('ActionCode', ActionCode);
您的索引将被推送到内存中,据此可以使文件的分辨率更快。
https://docs.mongodb.com/manual/indexes/
然后,由于 returned 文档的数量,它可能需要一些时间,只有 select 需要的字段,像这样 mongo 将只 return 到您的服务器需要字段而不是整个文档:
例如,如果您只需要姓名、年龄和类型字段:
Collection.find({name: "aaa"},{name:1, age:1, field:1}, function(err, response){
// returned documents here contains only following fields: _id, name, age, field
=======
尝试
Collection.find({}).select({_id: 1}).count(function(err, result)
或尝试聚合:
Collection.aggregate([{$group:{_id: null, count:{$sum :1}}])