【问题标题】:Mongoose/MongoDB - Reference another Schema without ObjectIdMongoose/MongoDB - 引用另一个没有 ObjectId 的模式
【发布时间】:2017-07-02 19:35:15
【问题描述】:

我目前有两个架构。

var carSchema = new mongoose.Schema({
    id: {
        type: Number,
        unique: true,
        required: true
    },
    make: {
        type: String,
        required: true
    },
    model: {
        type: String,
        required: true
    },
    year: {
        type: Number,
        required: true,
        min: config.MIN_YEAR,
        max: config.MAX_YEAR
    },
    color: {
        type: String
    },
    price: [
        // don't know what to put here...
    ],
 });


var priceSchema = new mongoose.Schema({
    car_id: {
        type: Number
    },
    amount: {
       type: Number,
       min: 0
    },
    year: {
       type: Number,
       min: config.MIN_YEAR,
       max: config.MAX_YEAR
    }
 });

从上面的两个模式中可以看出,我试图从汽车模式中引用价格模式。我知道您可以通过引用 ObjectId (_id) 来实现,但我需要通过 car_id 引用价格模式。原因是因为我正在从已经定义表头的 CSV 文件中读取所有数据。猫鼬有什么可能的方法来做这个参考吗?

【问题讨论】:

  • 为什么不同时保留car_id_id(新创建的)?因为使用其他 ID,您将无法从 populate 中受益。
  • @TalhaAwan 我将如何构建我的模式,使我能够同时利用car_id_id 来引用价格模式?这就是我不太明白的
  • 当您从 CSV 文件导入记录并从中创建价格对象时,您将同时拥有 car_id_id(猫鼬默认输入)。在汽车价格参考数组中输入_id
  • @TalhaAwan price: [ { type: mongoose.Schema.Types.ObjectId, ref: "Price" } ] 是我最初写的(这就是我认为你告诉我要做的),但我没有看到这如何让我参考@ 987654333@ == car_id
  • 如果您确实需要单独的集合,您可以在汽车中保留一系列价格参考,或在每个价格文档中保留汽车参考。无论哪种方式,引用都应该是 mongo id。 CSV 中的car_id 可以按原样存储在汽车文档中,但您可能不想将其用于引用。

标签: javascript node.js mongodb mongoose


【解决方案1】:

在汽车架构之上定义价格架构,然后将价格架构放入您的汽车架构。

...
color: {
    type: String
},
price: [priceSchema]
...

【讨论】:

    【解决方案2】:

    你可以这样做:

    var priceSchema = new mongoose.Schema({
        car_id: {
            type: Number
        },
        amount: {
           type: Number,
           min: 0
        },
        year: {
           type: Number,
           min: config.MIN_YEAR,
           max: config.MAX_YEAR
        }
     });
    

    然后将价格模型设为

    var Price = mongoose.model('Price', priceSchema);
    

    接着是:

    var carSchema = new mongoose.Schema({
        id: {
            type: Number,
            unique: true,
            required: true
        },
        make: {
            type: String,
            required: true
        },
        model: {
            type: String,
            required: true
        },
        year: {
            type: Number,
            required: true,
            min: config.MIN_YEAR,
            max: config.MAX_YEAR
        },
        color: {
            type: String
        },
        price: {
            type: mongoose.Schema.Types.ObjectId, ref: 'Price'
        },
     });
    

    然后将汽车模型设为:

    var Car = mongoose.model('Car', carSchema);
    

    这样就可以了。然后,您可以将查询运行为:

    Car.find({_id: 1})
    .populate('price')
    .exec(function(err, car) {
        // do stuff with your car objects
    });
    

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 2014-12-13
      • 2013-08-02
      • 2016-03-18
      • 2022-01-21
      • 2015-05-18
      • 2014-03-18
      • 2021-03-19
      • 2016-09-14
      相关资源
      最近更新 更多