Search code examples
node.jsmongodbmongoosemongoose-schema

What is the data type of _id in MongoDb


I want to create a user Schema using moongose. In user Schema i want to keep the ids of the blogs which written by the user. So what should be the data type of ids.

blogIds:{
    type: ???,
    require: true,
}

I thought that it can be String but I haven't tried.


Solution

  • You can get import the type ObjectId from mongoose:

    import mongoose, { Schema } from 'mongoose';
    
    
    .
    .
    
    blogIds:{
        type: [Schema.Types.ObjectId],
        require: true,
    }
    

    If not using ES6 import, then simply:

    type: [mongoose.Schema.Types.ObjectId]