1

I am trying to implement an array of ObjectId inside a schema in Mongoose. I searched in internet and I found that this should work :

import mongoose from 'mongoose';
import Schema from 'mongoose';

const UserSchema = mongoose.Schema({
  nickName: {
    type: String,
    unique: true,
    required: true,
  },
  follows: [{
    type: Schema.Types.ObjectId, //HERE
    ref: 'User',
    default: []
  }],
}, {
  strict: true,
});

const User = mongoose.model('User', UserSchema);
export default User;

or this

follows: {
          type: [Schema.Types.ObjectId], // HERE
          ref: 'User',
          default: []
  },

I know they are not exactly the same, but instead of working in both cases I have this error :

 Invalid schema configuration: `ObjectID` is not a valid type within the array `follows`.

I don't know why is he telling my that ObjectID (with capital "ID") is not valid as I didn't declare any of this.

How can I do an array of objectId ? I want an array of ObjectId by reference of the schema "User" with the people an user follow

[EDIT] As Bhanu Sengar mentionned in the comment, I had to put "mongoose" before the Schema.

[{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }] 

As Halil SAFAK said, I deleted the default value.

It also didn't work because I had conflicts between the two imports

import mongoose from 'mongoose';
import Schema from 'mongoose';

2 Answers 2

6

I have used mongooose Populate property check my code. This will help you to understand.

Category Schema

const mongoose  = require('mongoose');
const timestamps    = require('mongoose-timestamp');

const cateorySchema = new mongoose.Schema({
  category_name: {
    type: String,
    trim: true,
    required: true,
  },
  active: {
        type: Boolean,
        default: true,
    }
});

cateorySchema.plugin(timestamps); // automatically adds createdAt and updatedAt timestamps
module.exports = mongoose.model('Category',cateorySchema);

SubCategory Schema

'use strict'

const mongoose    = require('mongoose');
const timestamps    = require('mongoose-timestamp');

const subCategorySchema = new mongoose.Schema({
    categories:{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
    subcategorytitle:{
      type:String,
      trim:true,
      required: true
    },
    active: {
        type: Boolean,
        default: true
    }
});
subCategorySchema.plugin(timestamps); // automatically adds createdAt and updatedAt timestamps
module.exports = mongoose.model('Subcategory',subCategorySchema);

I hope this will help you. If you have any doubt let me know.

Sign up to request clarification or add additional context in comments.

3 Comments

Your example show a single ObjectId. I don't have any problem by implementing a single ObjectId in the Schema. But I do have one when I try to do an array of ObjectId.
If you want to use an array of ObjectId then is not a problem.You just to wap inside the array. Like categories:[{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' }]
This worked, thank you. I forgot to put the "mongoose" before the "Schema". Now it work. Fact is that it works without the "mongoose" when it's not an array, but it doesn't work when it is an array.
-2

Actually, the error is not in ObjectId but in your configuration. You define objectId in 'follows', and then you write array as default value, but this isn't actually the object type of ObjectId type, so here's the error. You will have no problem with the definition as follows.

follows: [{
    type: Schema.Types.ObjectId, //HERE
    ref: 'User'
  }],

or

follows: [Schema.Types.ObjectId],

In both definitions, the MongoDB populate query will work.

2 Comments

This is what I have tried before setting the default value and posting this thread, but I still have the same error...
does not solve the problem, the same error persists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.