我正在尝试在 Mongoose 的架构中实现一个 ObjectId 数组。我在互联网上搜索,我发现这应该有效:
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;Run Code Online (Sandbox Code Playgroud)
或这个
follows: {
type: [Schema.Types.ObjectId], // HERE
ref: 'User',
default: []
},Run Code Online (Sandbox Code Playgroud)
我知道它们并不完全相同,但不是在这两种情况下都工作,我有这个错误:
Invalid schema configuration: `ObjectID` is not a valid type within the array `follows`.
Run Code Online (Sandbox Code Playgroud)
我不知道他为什么告诉我 ObjectID(带有大写“ID”)无效,因为我没有声明任何这些。
我怎样才能做一个 objectId 数组?我想要一个 …