# MongoDB

Source : https://webgune.cloud/en-us/docs/db-mongodb · Mis à jour le 2026-10-27

# MongoDB

A document-oriented database, suited to schemas that change often or to naturally nested data.

## Connection string

```bash
MONGODB_URI=mongodb://user:password@host:27017/dbname
```

## Mongoose (Node.js)

```typescript
import mongoose from "mongoose";

await mongoose.connect(process.env.MONGODB_URI!);

const PostSchema = new mongoose.Schema({
  title:     { type: String, required: true },
  content:   String,
  createdAt: { type: Date, default: Date.now },
});

export const Post = mongoose.model("Post", PostSchema);
```

## PyMongo (Python)

```python
from pymongo import MongoClient

client = MongoClient(os.environ["MONGODB_URI"])
db = client.get_default_database()
posts = db.posts.find({"published": True})
```

## Indexes

Unlike a relational engine, MongoDB does not create an index on your filter fields by default. Remember to declare them explicitly as soon as your collections exceed a few thousand documents:

```javascript
await Post.collection.createIndex({ createdAt: -1 });
```
