MongoDB toArray 性能 [英] MongoDB toArray performance
问题描述
我正在尝试从 Mongo/Node 中的术语集合构建类别树,但首先我使用 $in 选择所有树元素:
I'm trying to build a category tree from a term collection in Mongo/Node, but first I select all tree elements using $in:
console.time('termsCol.find');
var terms = await termsCol.find({term_id: {'$in': flatTree}});
console.timeEnd('termsCol.find');
console.time('termsCol.toArray');
terms = await terms.toArray();
console.timeEnd('termsCol.toArray');
这执行:
termsCol.find: 0.162ms
termsCol.toArray: 30.910ms
我已阅读有关 SO 上 toArray 性能的帖子,但想知道是否有任何更改,因为这在页面请求期间占用了我的大部分时间.
我在该集合上有一个索引,它在 ~0.15 毫秒内返回 300 个术语,但是当我必须再等待 30 毫秒才能在 js 中进一步使用该数据时,这对我没有帮助.
如果没有办法改进 toArray 业务,我可能会创建一个缓存集合并将完整的术语树存储在那里(希望它们适合 16MB).
I've read posts about toArray performance on SO, but would like to know if anything has changed, because this takes most of my time during page request.
I have an index on that collection and it returns 300 terms within ~0.15ms, but that doesn't help me when I have to wait another 30ms to use that data further down in js.
If there's no way to improve this toArray business, I'll probably create a cache collection and store the complete term trees there (hope they fit 16MB).
推荐答案
reference http://mongodb.github.io/node-mongodb-native/2.0/tutorials/streams/您可以一个一个地流式传输结果,并且可以制作 id 数组.
reference http://mongodb.github.io/node-mongodb-native/2.0/tutorials/streams/ you can stream results one by one and can make array of ids.
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
var col = db.collection('terms');
var ids = []
var findCursor = col.find({term_id: {'$in': flatTree}});
findCursor.on("data", function(data) {
ids.push(data._id)
});
findCursor.on("end", function(data) {
// let's finish
console.log(ids)
});
});
我没有检查时间,但确定它应该少于(termsCol.find:0.162ms +termCol.toArray: 30.910ms)
i didn't check time but sure it should less then (termsCol.find: 0.162ms + termsCol.toArray: 30.910ms)
这篇关于MongoDB toArray 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!