【问题标题】:NodeJS/Mongoose schema array of ObjectIdObjectId 的 NodeJS/Mongoose 模式数组
【发布时间】:2019-07-18 13:34:50
【问题描述】:

我正在尝试在 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;

或者这个

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

我知道它们并不完全相同,但我没有在这两种情况下工作,而是出现了这个错误:

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

我不知道他为什么告诉我 ObjectID(大写“ID”)无效,因为我没有声明任何这些。

我怎样才能做一个 objectId 数组? 我想要一个 ObjectId 数组,通过引用模式“用户”与用户关注的人

[编辑] 正如 Bhanu Sengar 在评论中提到的那样,我必须在 Schema 之前加上“mongoose”。

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

正如 Halil SAFAK 所说,我删除了默认值。

它也不起作用,因为我在两个导入之间存在冲突

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

【问题讨论】:

    标签: arrays node.js mongoose schema objectid


    【解决方案1】:

    我使用猫鼬填充属性检查我的代码。 这将有助于您理解。

    类别架构

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

    子类别架构

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

    我希望这会对你有所帮助。如果您有任何疑问,请告诉我。

    【讨论】:

    • 您的示例显示了一个 ObjectId。通过在 Schema 中实现单个 ObjectId 没有任何问题。但是当我尝试做一个 ObjectId 数组时,我确实有一个。
    • 如果你想使用 ObjectId 的数组,那不是问题。你只需在数组内部进行 wap。喜欢的类别:[{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' }]
    • 这行得通,谢谢。我忘了把“猫鼬”放在“架构”之前。现在它起作用了。事实是它不是数组时没有“mongoose”可以工作,但是当它是数组时它不起作用。
    【解决方案2】:

    实际上,错误不在 ObjectId 中,而是在您的配置中。您在'follows'中定义objectId,然后将数组写入默认值,但这实际上不是ObjectId类型的对象类型,所以这里是错误的。如下定义,你不会有任何问题。

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

    follows: [Schema.Types.ObjectId],
    

    在这两种定义中,MongoDB 填充查询都可以工作。

    【讨论】:

    • 这是我在设置默认值并发布此线程之前尝试过的,但我仍然有同样的错误......
    • 没有解决问题,同样的错误依然存在。
    猜你喜欢
    • 2016-06-12
    • 2016-06-13
    • 2015-05-02
    • 2017-11-22
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多