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 in Node-js by Ashutosh
• 20,830 points
84 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 by anonymous

Related Questions In Node-js

0 votes
1 answer
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 in Node-js by Kavya
44 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 in Node-js by Kavya
66 views
0 votes
1 answer
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 in Node-js by Kavya
46 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 in Node-js by Kavya
55 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 in Node-js by Kavya
97 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 in Node-js by Kavya
63 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 in Node-js by Kavya
67 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