Just seeing if anyone has experience with the following. If I want to store a nested JSON object which may have anywhere from 10 to 500 key:string pairs, is it better to store the nested JSON object as a string or keep it as an object? What will the memory penalty be like for keeping the value as a JSON object rather than string?
1 Answer
You can check the size of document with Object.bsonsize() in the mongoshell
For example:
> Object.bsonsize({})
5
> Object.bsonsize({a:''})
13
> Object.bsonsize({a:'',b:''})
21
> Object.bsonsize({a:'',b:'',c:''})
29
> Object.bsonsize({a:{a:'',b:'',c:''}})
37
> Object.bsonsize({a:{a:'',b:''}})
29
> Object.bsonsize({a:{a:''}})
21
> Object.bsonsize({a:"{a:'',b:'',c:''}"})
29
> Object.bsonsize({a:"{a:'',b:''}"})
24
> Object.bsonsize({a:"{a:''}"})
19
> Object.bsonsize({a:""})
13
> Object.bsonsize({ab:""})
14
> Object.bsonsize({abc:""})
15
It looks like for me that empty doc is 5 byte one empty string is 7 byte plus each character in the name is 1 byte and each in the content is 1 byte.
2 Comments
booyaa
Just to add to @attish's research, you should measure the memory usage on the application side using the object vs. adding the JSON.parse call on the string.
Vishal Kumar Sahu
So it will take same memory as that in the file system. Better hashkey the json object compress and store in the file system.