How to get the size of single document in Mongodb?

前端 未结 5 1563
情歌与酒
情歌与酒 2020-11-28 03:34

I encountered a strange behavior of mongo and I would like to clarify it a bit...
My request is simple as that: I would like to get a size of single document in collect

5条回答
  •  鱼传尺愫
    2020-11-28 03:54

    In the previous call of Object.bsonsize(), Mongodb returned the size of the cursor, rather than the document.

    Correct way is to use this command:

    Object.bsonsize(db.test.findOne())
    

    With findOne(), you can define your query for a specific document:

    Object.bsonsize(db.test.findOne({type:"auto"}))
    

    This will return the correct size (in bytes) of the particular document.

提交回复
热议问题