0% found this document useful (0 votes)
17 views14 pages

User Schema

The document provides an overview of Mongoose User Schema, including its initialization and validation features. It details the installation and setup of MongoDB, along with basic CRUD operations such as creating, reading, updating, and deleting user documents. Additionally, it covers advanced querying techniques like finding documents, using projections, and sorting results.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

User Schema

The document provides an overview of Mongoose User Schema, including its initialization and validation features. It details the installation and setup of MongoDB, along with basic CRUD operations such as creating, reading, updating, and deleting user documents. Additionally, it covers advanced querying techniques like finding documents, using projections, and sorting results.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

User Schema

Exploring Mongoose User Schema and Its Validation Features


•Initialization:
Schema initialization is a crucial step where the
database structure is created using Data Definition
Language (DDL) statements (e.g., CREATE
TABLE, CREATE INDEX).
01
Mongoose
User Schema
const mongoose = require('mongoose’);
const userSchema = new
mongoose.Schema({
name: { type: String, required: true },
age: { type: Number, min: 18 },
email: { type: String, unique: true }
});

const User = mongoose.model('User',


userSchema);
Installation and Setup

bash
npm install mongodb

Javascript
const { MongoClient } =
require('mongodb'); const uri =
"mongodb://localhost:27017"; const
client = new MongoClient(uri);
Connecting to MongoDB

javascript
async function run() {
try {
await client.connect();
console.log("Connected to
MongoDB");
}
finally { await client.close();
}}
run().catch(console.dir);
Basic CRUD Operations
Create:
javascript
await
db.collection('users').insertOne({ name:
"Vaishnavi", age: 21 });

Read:
javascript
const user = await
db.collection('users').findOne({ name:
"Vaishnavi" });
Update:
javascript
await
db.collection('users').updateOne({ name:
"Vaishnavi" }, { $set: { age: 22 } });

Delete:
javascript
await
db.collection('users').deleteOne({ name:
"Vaishnavi" });
Reading and writing from
mongo db
a) Insert One Document
javascript
db.users.insertOne({ name: "Aarav", age: 25,
email: "[email protected]" });

b) Insert Multiple Documents


javascript
db.users.insertMany([ { name: "Meera", age:
22 }, { name: "Kiran", age: 24 } ]);
c) Update a Document
javascript
db.users.updateOne( { name: "Aarav" }, { $set:
{ age: 26 } } );

d) Replace a Document
javascript
db.users.replaceOne( { name: "Meera" }, { name:
"Meera", age: 23, email: "[email protected]" } );
a) Find One Document

javascript
db.users.findOne({ name: "Aarav" });

b) Find Multiple Documents

javascript
db.users.find({ age: { $gt: 21 } }).toArray();
c) Using Projection (select specific fields)

javascript
db.users.find({}).project({ name: 1, _id: 0 });

d) Using Sorting, Limiting, and Skipping

javascript
db.users.find({}).sort({ age: -1 }).limit(5).skip(2);

You might also like