2

I am using nodejs and mongoose to save data in mongodb. I have a schema like this. I want to save an array which has these objects as array elements

const gatePassSchema = new mongoose.Schema({
    sno: {
        type: String,
        required: true
    },
    modeOfTransport: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    quantity: {
        type: Number,
        required: true
    },
    unit: {
        type: String,
        required: true
    },
    issuedTo: {
        type: String,
        required: true
    },
    dateOfReturn: {
        type: Date,
        required: true
    },
    from: {
        type: String,
        required: true
    },
    to: {
        type: String,
        required: true
    },
    reason: {
        type: String,
        required: true
    },
    remark: {
        type: String,
        required: true
    },
    incomingRef: {
        type: String,
        required: true
    }

})

The request body coming to the server looks like this:

 [ { sno: '1',
    modeOfTransport: 'oijoi',
    description: 'oiouiu',
    quantity: 5,
    unit: 'number',
    issuedTo: 'giug',
    dateOfReturn: '2020-07-12T18:30:00.000Z',
    from: 'iuhiu',
    to: 'hiuhi',
    reason: 'hiu',
    remark: 'ih',
    incomingRef: 'iuhilk' },
  { sno: '2',
    modeOfTransport: 'avvss',
    description: 'uihiuhiu',
    quantity: 6,
    unit: 'packet',
    issuedTo: 'giukh',
    dateOfReturn: '2020-07-05T18:30:00.000Z',
    from: 'iuhi',
    to: 'uihnvn',
    reason: 'lop',
    remark: 'ytfyvh',
    incomingRef: 'psd' } ]

I want it to save this data format: [{gatePassSchema},{gatePassSchema}....] Most implementations I found online show how to turn an element in the model into an array, but what if I want to use the whole model as an array?

1 Answer 1

2

First you will need to convert your schema into a model:

var GatePass = mongoose.model('GatePass', gatePassSchema);

after that you just need to run array.map on your array and create instances of your model.

Something like this:

yourArray.map(element => new GatePass(element));

This should return an array where your JSON object should be converted into mongoose models. Is this what you needed?

More on array map:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! The array.map worked for me. It saves as separate instances in the db. I used a for loop for this, is there any better way? for(i=0; i<gatePasses.length; i++) { try { await gatePasses[i].save() .then(item => { // res.json(item) console.log('New GatePass added w/ id: ',item.id) })
You could use yourArray.forEach. for that, you can read more on: medium.com/@JeffLombardJr/…
This does work, but is there a way to save this as an array instead of saving the array elements as separate instances? If I save them as separate instances, it makes it very hard to write a get api when i have to retrieve them since I want to retrieve them as an array
You can put all that in the first array map I've posted. Instead of just calling new GatePass you can expand it to also save the element and execute the request. At the end you then return the results. In case you need the array of gatePass elements for later then you can also call a separate array map on that array and with that you will get an array of results that you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.