Skip to content

Instantly share code, notes, and snippets.

@lineus lineus/charliecode3.js Secret
Created Aug 27, 2018

What would you like to do?
using bson to calculate object size
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
const BSON = require('bson');
const bson = new BSON();
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const schema = new Schema({
buf: Buffer
const Test = mongoose.model('test', schema);
const str = 'a'.repeat(16777175);
const test = new Test({ buf: Buffer.from(str)});
async function run() {
await conn.dropDatabase();
let doc = await Test.findOne().lean();
let sizeMB = bson.calculateObjectSize(doc) / (1024 * 1024);
console.log(`doc is ${sizeMB} MB`);
return conn.close();
$: ./charliecode3.js
doc is 16 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.