Last active
September 25, 2017 21:51
-
-
Save gitawego/321098b0fef07c9668c95deadf06c9ea to your computer and use it in GitHub Desktop.
bson serialization and deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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