Categories
JavaScript Answers

How to set ObjectId as a data type in Node Mongoose?

Spread the love

To set ObjectId as a data type in Node Mongoose, we set the value to ObjectId.

For instance, we write

const mongoose = require("mongoose");

const Schema = mongoose.Schema,
  ObjectId = Schema.ObjectId;

const SchemaProduct = new Schema({
  categoryId: ObjectId,
  title: String,
  price: Number,
});

to set the categoryId property to ObjectId to make categoryId an object ID field.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.