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';