简体   繁体   中英

How Can I make mongo db schema for this?

i am trying to scrape data and and send to my MonogoDB database but I am new to this and also confused in making Schema for my database.This is what I am trying to send to my database and I tried a lot searching but could not find proper solution. I was able to make schema but it was for only one sub but now I have more than one sub. Can it be done by making model or schema for the sub list?

{
        sub: [
          {
            code: 'ELA2010',
            sessionals: '54',
            exam: '26',
            total: '80',
            grace: '1',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'MEH2450',
            sessionals: '56',
            exam: '26',
            total: '82',
            grace: '0',
            grades: 'A',
            range: 'A+:85-100, A:75-84, B+:61-74, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'EEC2120',
            sessionals: '64',
            exam: '26',
            total: '90',
            grace: '0',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'EEC2210',
            sessionals: '53',
            exam: '26',
            total: '79',
            grace: '0',
            grades: 'A',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'EEC2310',
            sessionals: '54',
            exam: '26',
            total: '80',
            grace: '1',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'EEC2510',
            sessionals: '61',
            exam: '26',
            total: '87',
            grace: '0',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60, C:41-50, D:35-40'
          },
          {
            code: 'EEC2920',
            sessionals: '61',
            exam: '26',
            total: '87',
            grace: '0',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60'
          },
          {
            code: 'EEC2930',
            sessionals: '61',
            exam: '26',
            total: '87',
            grace: '0',
            grades: 'A+',
            range: 'A+:81-100, A:71-80, B+:61-70, B:51-60'
          }
        ],
        info: {
          facno: '17EHB116',
          eno: 'GK6147',
          name: 'Steve Jhon',
          cpi: '8.91',
          spi: '9.714'
        }
      }

This is what I was trying to do but I didn't know how make schema for the sub array--

const mongo = require('mongoose');
const result = mongo.Schema({
sub:[
{
    code: { type: String }, sessionals: { type: String },
    exam: { type: String }, total: { type: String },
    grace: { type: String }, grades: { type: String }, range: { type: String}
}],
info: {
    facno: { type: String },
    eno: { type: String }, name: { type: String }, cpi: { type: String }, 
    spi: { type: String }
}
});
   module.exports = Result = mongo.model('result', result);

Please tell me how can I make schema for this:((

As specified in one of the answer, you can use mongoose package with node.js.

You can create schema in following way,

 new mongoose.Schema({ sub:[ new mongoose.Schema({ code: String, sessionals: String, exam: String, total: String, grace: String, grades: String, range: String })], info: { facno: String, eno: String, name: String, cpi: String, spi: String } })

You would make a Schema and then use it to save to your MongoDB instance.

I would suggest using a package called Mongoose . It makes MongoDB much simpler.

To make a Schema to support multiple subs you can do something like this:

new mongoose.Schema({
    sub1: [{
            code: String
            sessionals: String
            exam: String
            total: String
            grace: String
            grades: String
            range: String
        },
        {
            code: String
            sessionals: String
            exam: String
            total: String
            grace: String
            grades: String
            range: String
        }
    ],
    sub2: [{
            code: String
            sessionals: String
            exam: String
            total: String
            grace: String
            grades: String
            range: String
        },
        {
            code: String
            sessionals: String
            exam: String
            total: String
            grace: String
            grades: String
            range: String
        }
    ]
});

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