【问题标题】:how to create a mongoose model with list of dictionaries?如何使用字典列表创建猫鼬模型?
【发布时间】:2019-11-11 19:20:14
【问题描述】:

我正在尝试为对象如下所示的集合创建模型,如何在字典列表中的 mongoose 中声明 clonedChangesdetailslist[String] 似乎不是正确的事情?

{
    "_id" : ObjectId("6d17d2dd84d4734eea82989f"),
    "orgChange" : "62369696",
    "created_on" : ISODate("2019-06-29T14:06:20.686Z"),
    "clonedChangesdetailslist" : [ 
        {
            "clonedChange" : "62392779",
            "clonedStatus" : "PASS",
            "clonedChangeFinalStatus" : "PASS",
            "updatedFailedReason" : "N/A",
            "clonedChangeFinalStatusReason" : "N/A",
            "updateStatus" : "PASS",
            "clonedStatusfailReason" : "N/A"
        }, 
        {
            "clonedChange" : "62392793",
            "clonedStatus" : "PASS",
            "clonedChangeFinalStatus" : "PASS",
            "updatedFailedReason" : "N/A",
            "clonedChangeFinalStatusReason" : "N/A",
            "updateStatus" : "PASS",
            "clonedStatusfailReason" : "N/A"
        }
    ]
}

mongodb模型

const mongoose = require('mongoose');
const { Schema } = require('mongoose');

const change_cloning_Schema= new Schema({

    orgChange: String,
    created_on: String,
    clonedChangesdetailslist:[String]


},
{
  collection: 'change_cloning',
  timestamps: { createdAt: true, updatedAt: true },
});

module.exports = mongoose.model('change_cloning', change_cloning_Schema);

【问题讨论】:

    标签: node.js mongodb mongoose mean-stack


    【解决方案1】:

    您可以将clonedChangesdetailslist 定义为Array of Objects

    试试这个:

    const change_cloning_Schema= new Schema({
    
        orgChange: String,
        created_on: String,
        clonedChangesdetailslist:[{
            clonedChange : String,
            clonedStatus : String,
            clonedChangeFinalStatus : String,
            updatedFailedReason : String,
            clonedChangeFinalStatusReason : String,
            updateStatus : String,
            clonedStatusfailReason : String
        }]
    
    
    },
    {
      collection: 'change_cloning',
      timestamps: { createdAt: true, updatedAt: true },
    });
    

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2016-03-09
      • 2012-04-22
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多