Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a mongoose model with list of dictionaries?

I am trying to create a model for a collection whose objects look like below,how do declare clonedChangesdetailslist in mongoose which is a list of dictionaries?[String] doesn't seem to be the right thing?

{
    "_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 model

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);
like image 932
Ritz Avatar asked Dec 05 '25 08:12

Ritz


1 Answers

You can define clonedChangesdetailslist as an Array of Objects.

Try this :

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 },
});
like image 60
Ravi Shankar Bharti Avatar answered Dec 07 '25 21:12

Ravi Shankar Bharti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!