English 中文(简体)
我怎么能找到正在占用大量空间的微型信贷收集的内容?
原标题:How can I find elements of a MongoDB collection that are taking up a large amount of space?

If I have a collection with thousands of elements, is there a way I can easily find which elements are taking up the most space (in terms of MB)?

最佳回答

没有这方面的内在询问,你必须重新收集资料,收集每份文件的篇幅,然后进行分类。 如何开展工作:

var cursor = db.coll.find(); 
var doc_size = {}; 
cursor.forEach(function (x) { 
    var size = Object.bsonsize(x); 
    doc_size[x._id] = size;
});

At this point you ll have a hashmap with document ids as keys and their sizes as values. Note that with this approach you will be fetching the entire collection over the wire. An alternative is to use MapReduce and do this server-side (inside mongo):

> function mapper() {emit(this._id, Object.bsonsize(this));}
> function reducer(obj, size_in_b) { return { id : obj, size : size_in_b}; }
>
> var results = db.coll.mapReduce(mapper, reducer, {out : {inline : 1 }}).results
> results.sort(function(r1, r2) { return r2.value - r1.value; })

在线:1 告诉mongo不要为成果设立临时收集,所有工作都将留在援助团。

我收集的样本:

[
    {
        "_id" : ObjectId("4ce9339942a812be22560634"),
        "value" : 1156115
    },
    {
        "_id" : ObjectId("4ce9340442a812be24560634"),
        "value" : 913413
    },
    {
        "_id" : ObjectId("4ce9340642a812be26560634"),
        "value" : 866833
    },
    {
        "_id" : ObjectId("4ce9340842a812be28560634"),
        "value" : 483614
    },
       ...
    {
        "_id" : ObjectId("4ce9340742a812be27560634"),
        "value" : 61268
    }
]
> 
问题回答

Figured this out! I did this in two steps using Object.bsonsize():

db.myCollection.find().forEach(function(myObject) {
    db.objectSizes.save({object_id: object._id, size: Object.bsonsize(chain)});
});

db.objectSizes.find().sort({size: -1}).limit(5).pretty();  




相关问题
Is HashMap in Java collision safe

I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>> . What happens if the ...

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

PLSQL Collections - how to use table of records?

I m new to PL/SQL and I m trying to use a table of records, but I don t know how to use this feature. What is the problem? DECLARE TYPE TIP IS RECORD ( F1 ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...

热门标签