MongoDB as a JSON Document Store
MongoDB stores data as BSON (Binary JSON) documents — a superset of JSON that adds types like ObjectId, Date, and BinData. You can insert standard JSON objects directly using the MongoDB driver.
Schema Design Approaches
- Embedded Documents: Store related data inside a single document for read performance (e.g. user with address embedded).
- References: Store ObjectId references for many-to-many or frequently-updated related data.
- Mongoose ODM: Define schemas with validation rules using our JSON to Code generator.
// MongoDB Node.js Driver — Insert JSON documents
const { MongoClient } = require('mongodb');
const client = new MongoClient(process.env.MONGODB_URI);
const db = client.db('myapp');
// Insert a single JSON document
const result = await db.collection('users').insertOne({
name: 'Alice',
email: 'alice@example.com',
roles: ['admin', 'editor'],
createdAt: new Date()
});
// Insert multiple JSON documents
await db.collection('users').insertMany([
{ name: 'Bob', email: 'bob@example.com', roles: ['viewer'] },
{ name: 'Carol', email: 'carol@example.com', roles: ['editor'] }
]);
// Query JSON documents
const admins = await db.collection('users')
.find({ roles: { $in: ['admin'] } })
.toArray();