Binary (Binary Data)

Last updated: 2026-10-09

Binary (Binary Data)

Binary stores images, files, encrypted data, and UUIDs — more compact than Base64 strings, with subtypes (0 generic, 2 old binary, 3 UUID legacy, 4 UUID, 7 encrypted). Store directly within the 16MB document limit; use GridFS for larger files. In mongosh construct with `BinData(subtype, 'base64')` or `Binary.createFromBase64('...')`.

CategoryBSON Types
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

BinData(<subtype>, "<base64>")
UUID("<uuid string>")
Binary.createFromBase64("<base64>")

Returns:Returns a Binary BSON value

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// Binary: store binary data
use shop
db.files.insertOne({ name: 'logo.png', data: BinData(0, 'iVBORw0KGgo=') })

Output:

{ _id: ObjectId('...'), name: 'logo.png', data: Binary.createFromBase64('iVBORw0KGgo=') }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QCan I store images directly in MongoDB?
AUnder 16MB, yes — use a Binary field; beyond that, use GridFS or object storage.
QHow is a UUID stored in MongoDB?
AAs Binary subtype 4 (UUID); drivers convert automatically.
QBinary or Base64 string?
AUse Binary for binary data (compact, subtypes); String for text.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏