【问题标题】:How to write a schema for this code in node .js如何在 node .js 中为此代码编写架构
【发布时间】:2020-06-01 02:32:34
【问题描述】:
 _id :hhjjdfghjkkmdfghjkl8888
  orderDetail:
  [
     {
        fitoName:,
        fitoCode:,
        ProviderName:
        ProviderCode:
        Quantity:
        storUrl:fitotouch.com/fivestart/chinasore
     },
     {
        fitoName:,
        fitoCode:,
        ProviderName:
        ProviderCode:
        Quantity:
        storUrl:fitotouch.com/fivestart/chinasore
     }  
  ]
}

【问题讨论】:

  • 你应该更精确。
  • 你的问题一点都不清楚,请给你的问题添加一些解释。

标签: node.js mongoose nested


【解决方案1】:

你可以用这个方法来做

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/mongoose_basics');

var authorSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    name: {
            firstName: String,
        lastName: String
    },
    biography: String,
    twitter: String,
    facebook: String,
    linkedin: String,
    profilePicture: Buffer,
    created: { 
        type: Date,
        default: Date.now
    }
});

var Author = mongoose.model('Author', authorSchema);

使用此参考网址:https://code.tutsplus.com/articles/an-introduction-to-mongoose-for-mongodb-and-nodejs--cms-29527

【讨论】:

    【解决方案2】:

    你可以这样做:

    const mongoose = require("mongoose");
    
    const OrderDetails = mongoose.Schema(
      {
        fitoName: String,
        fitoCode: String,
        providerName: String,
        providerCode: String,
        quantity: Number,
        storUrl: String
      },
      { _id: false } // If you don't want an ID to be created by MongoDB
    );
    
    const Orders = mongoose.model(
      "orders",
      new mongoose.Schema(
        {
          orderDetail: {
            type: [OrderDetails],
            required: true
          }
        },
        { timestamps: true }
      )
    );
    

    【讨论】:

    • @Muhmmad Zeshan 如何在 node.js 中保存这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多