What are MongoDB data types and how do you define them in a schema

0 votes
With the help of code example can you tell me What are MongoDB data types, and how do you define them in a schema?
Feb 22, 2025 in Node-js by Ashutosh
• 33,350 points
516 views

1 answer to this question.

0 votes

MongoDB supports various data types, including:

String (String) – Text values.

Number (Number) – Integers & floating points.

Boolean (Boolean) – true or false.

Array (Array) – Lists of values.

Object (Object) – Embedded documents.

ObjectId (ObjectId) – Unique document identifier.

Date (Date) – Timestamps.

Defining Data Types in Mongoose 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 },

  isActive: { type: Boolean, default: true },

  hobbies: { type: [String] }, // Array of Strings

  createdAt: { type: Date, default: Date.now },

});

const User = mongoose.model("User", userSchema);

answered Feb 23, 2025 by anonymous

Related Questions In Node-js

0 votes
1 answer

What are the Typescript types for React checkbox events and handlers, or how do I define them?

In TypeScript, handling checkbox events in React ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
2,464 views
0 votes
1 answer

How do you design a schema for tree structures in MongoDB?

Designing a schema for tree structures in ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
549 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
490 views
0 votes
1 answer

How do you pass and use URL parameters in a React-Router route?

Passing and Using URL Parameters in React ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
523 views
0 votes
1 answer

What is the difference between RDBMS relationships and MongoDB’s data model?

Feature RDBMS (SQL Databases) MongoDB (NoSQL Document Database) Data Structure Tables ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
481 views
0 votes
1 answer

Write a query for a compound index to optimize a search operation in MongoDB.

A compound index improves search performance by ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
541 views
0 votes
1 answer

How do you handle concerns for write operations in MongoDB?

Write operations in MongoDB need to be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
546 views
0 votes
1 answer

Write a query to fetch specific fields using MongoDB projections.

Fetching Specific Fields Using MongoDB Projections MongoDB projections ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
539 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
558 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP