Mongoose के साथ MongoDB के Basics
Mongoose MongoDB collections को एक schema और Node में आसान model methods देता है।
In this page:
Syntax
const mongoose = require('mongoose');
await mongoose.connect('mongodb://host/database');
const schema = new mongoose.Schema({ field: Type });
const Model = mongoose.model('Name', schema);
Mongoose Basics के साथ MongoDB
Mongoose Schemas define करता है, इन्हें Models में compile करता है, और mongoose.connect से connect करता है। Models create, find, findById, updateOne, और deleteOne offer करते हैं। Schemas MongoDB के flexible documents के ऊपर validation और defaults add करते हैं।
Note:
models इस्तेमाल करने से पहले mongoose.connect await करें।
उदाहरण: MongoDB with Mongoose basics
$ npm install mongoose
const mongoose = require("mongoose");
await mongoose.connect(process.env.MONGODB_URI);
const User = mongoose.model("User", new mongoose.Schema({ name: { type: String, required: true }, age: Number }));
await User.create({ name: "Ada", age: 36 });
console.log(await User.find({ age: { $gt: 30 } }));
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- connect करने से पहले models इस्तेमाल करना
- required fields define करना भूल जाना
- validation errors handle न करना
चैप्टर सारांश
- Schema fields describe करता है
- Model queries देता है
- connect database खोलता है
- Validation built in है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: