1

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 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
So it will take same memory as that in the file system. Better hashkey the json object compress and store in the file system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.