简体   繁体   中英

Can't Store Nested JSON Array to MongoDB Using Mongoose

I have JSON on Postman like this:

{
    "hostname": [
        {
            "item": [
                {
                    "system": "10l313",
                    "severity": "2"
                },
                {
                    "system": "2131414",
                    "severity": "3"
                }
            ]
        },
        {
            "item": [
                {
                    "system": "4234235",
                    "severity": "4"
                }
            ]
        }
    ]
}

I want to create new collections in mongodb from json above. It's just a little picture of the actual json array, the above json array can contain an enormous array. I am confused how to save as many json arrays using mongoose, do i have to loop as much as array length or is there other easier way?

mongoose schema:

var ItemSchema =  new Schema({
    _id: mongoose.Schema.Types.ObjectId,

    system: {
        type: String
    },

    severity: {
        type: Number
    }
})

var VulnSchema = new Schema({

    hostname: [{
        item: [{ItemSchema}]
    }]

});

controller:

exports.create_vulnerabilities = function (req, res) {
    var vuln = new Vuln ({
        _idFIle: mongoose.Types.ObjectId(),
        hostname: req.body.hostname
    });         
    vuln.save()
      .then(result => {
          res.status(201).json({
              result
          });
      })
      .catch(err => {
          console.log(err);
          res.status(500).json({
              error: err
          });
      });

};

I have tried running my code but the result is like this. The problem is system and severity attribute are not stored in mongodb.

{
    "_id" : ObjectId("5b4c39a301651a0fc047bec7"),
    "hostname" : [ 
        {
            "_id" : ObjectId("5b4c39a301651a0fc047beca"),
            "item" : [ 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047becc")
                }, 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047becb")
                }
            ]
        }, 
        {
            "_id" : ObjectId("5b4c39a301651a0fc047bec8"),
            "item" : [ 
                {
                    "_id" : ObjectId("5b4c39a301651a0fc047bec9")
                }
            ]
        }
    ],
    "__v" : 0
}

Please help me. thank you

Change

var VulnSchema = new Schema({
    hostname: [{
       item: [{ItemSchema}]
    }]
});

to

var VulnSchema = new Schema({
     hostname: [{
        item: [ItemSchema]
    }]
});

Example try running this:

const mongoose = require('mongoose');

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

const Schema = mongoose.Schema,ObjectId = Schema.ObjectId;

var ItemSchema =  new Schema({
    _id: mongoose.Schema.Types.ObjectId,

    system: {
        type: String
    },

    severity: {
        type: Number
    }
})

var VulnSchema = new Schema({
    hostname: [{
        item: [ItemSchema]
    }]
});

const Vuln = mongoose.model('Vuln', VulnSchema);

var hostname = [
    {
        "item": [
            {
                "system": "10l313",
                "severity": "2"
            },
            {
                "system": "2131414",
                "severity": "3"
            }
        ]
    },
    {
        "item": [
            {
                "system": "4234235",
                "severity": "4"
            }
        ]
    }
]

var vuln = new Vuln ({
    _idFIle: mongoose.Types.ObjectId(),
    hostname: hostname
});

vuln.save()
    .then(result => {
       console.log(JSON.stringify(result))
})
    .catch(err => {
       console.log(err);}
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM