猫鼬模式数组中是否可以包含两种类型的对象

问题描述

我正在构建一个电子商务应用程序,这是我的订单架构。

const mongoose = require("mongoose");

const orderSchema = new mongoose.Schema({
    buyer: {
        type: mongoose.Schema.Types.ObjectId,ref: "buyer",required: true
    },items: [{
        item: {
            type: mongoose.Schema.Types.ObjectId,ref: "item",},quantity: {
        type: Number,default: 1
    }}
    ],seller: {
        type: mongoose.Schema.Types.ObjectId,ref: "seller",location: {
        type: {
            type: "String",enum:['Point']
        },coordinates: {
            type: [Number],index: '2dsphere'
        }
    },sendAt:{
        type: Date,default: Date.Now
    } 
});

const orderModel = mongoose.model("orders",orderSchema);
module.exports = orderModel;

我想要一个包含项目参考编号和数量的数组。 但是使用上面的架构,当我输入数据时,每个项目都充当另一个子文档并具有_id。 Query response image

解决方法

我找到了解决方案:

    order: [
    {
      _id: false,item: {
        type: mongoose.Schema.Types.ObjectId,ref: "items",required: true,},quantity: { type: Number },],

_id: false 将阻止子文档为子文档创建另一个 id。