Skip to content

Instantly share code, notes, and snippets.

@gitawego
Last active September 25, 2017 21:51
Show Gist options
  • Select an option

  • Save gitawego/321098b0fef07c9668c95deadf06c9ea to your computer and use it in GitHub Desktop.

Select an option

Save gitawego/321098b0fef07c9668c95deadf06c9ea to your computer and use it in GitHub Desktop.
bson serialization and deserialization
const BSON = require('bson');
const fs = require('fs');
const bson = new BSON();
const file = './test.bson';
const bf = fs.readFileSync(file);
const len = bf.length;
let bfIdx = 0;
const items = [];
console.log('idx',bfIdx,len);
while(bfIdx < len){
console.log('bfiDX',bfIdx);
bfIdx = bson.deserializeStream(bf,bfIdx,1,items,items.length);
console.log('items',items);
}
const BSON = require('bson');
const MAXSIZE = 1024 * 1024 * 17;
const fs = require('fs');
const bson = new BSON();
const file = './test.bson';
let bf = new Buffer(MAXSIZE);
const data = [{
a:1,
b:new Date('2019-01-01')
},{
a:2,
b:new Date('2019-01-02')
},{
a:3,
b:new Date('2019-01-03')
}];
const chunks = [];
data.forEach((item,i)=>{
console.log(item,i);
const doc = Buffer.from(bson.serialize(item));
chunks.push(doc);
});
bf = Buffer.concat(chunks);
console.log(bf.length);
fs.writeFileSync(file,bf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment