Member-only story
MongoDB Architecture
MongoDB is a popular NoSQL database designed for high performance, high availability, and easy scalability. It stores data in flexible, JSON-like documents, making it easy to work with structured, semi-structured, and unstructured data.
- Database: A container for collections.
- Collection: A group of MongoDB documents.
- Document: A set of key-value pairs (similar to JSON objects).
CRUD Operations
CRUD stands for Create, Read, Update, and Delete. These are the basic operations for interacting with data in MongoDB.
Create
To insert a new document into a collection, you use the insertOne()
or insertMany()
methods.
insertOne
db.collection('users').insertOne({ name: 'Alice', age: 25 });
Auto-Generated _id Field If you don’t specify an _id
field, MongoDB automatically generates a unique identifier for each document. To specify your own _id
value, you can include it in the document.
db.collection('users').insertOne({ _id: 1, name: 'Alice', age: 25 });
Make sure to handle duplicate _id
values to avoid conflicts.