Home > Mobile >  Is there way to add array of objectid in mongodb schema?
Is there way to add array of objectid in mongodb schema?

Time:12-26

I wanted to create a schema , with multiple object id. For example

const derivedSchema = new mongoose.Schema(
  {
    user: {type: ObjectId, ref: "User"},
    collections: [{type: ObjectId, ref="collection"}],
);
 

Where collection is reference to another mongoose model. I'm not sure if it is way to write userIds in array. Is this correct way or is there another way? Thank you

CodePudding user response:

var mongoose = require('mongoose');
var Schema=mongoose.Schema;

const derivedSchema = new mongoose.Schema(
  {
    user: {
           type: Schema.Types.ObjectId,
           ref: "User"
    },
    collections:{
        type:[Schema.Types.ObjectId],
        ref:"collection"
    }
);
  • Related