0
  • I have an existing array which looks like this
{
  _id: 5f14675a4fb8565fb89ec338,
  name: 'sample5',
  email: 'sample5@test.com',
  rollno: 9,
  city: [
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 1',
      citycode: '000000'
    }
  ],
  __v: 0
}
  • So I want to append the data to the existing array which is
city: [
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 2',
      citycode: '000002'
    }
  ],
  • So my final db should look like this
{
  _id: 5f14675a4fb8565fb89ec338,
  name: 'sample5',
  email: 'sample5@test.com',
  rollno: 9,
  city: [
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 1',
      citycode: '000000'
    },
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 2',
      citycode: '000002'
    },
  ],
  __v: 0
}
  • For now I am just able to update my existing data where I show the cityname and citycode and if the user make any changes it is reflected in the database.
  • Code I am using to update the database
// Update Student
router.route('/update-student/:id').put((req, res, next) => {
  studentSchema.findByIdAndUpdate(req.params.id, {
    $set: req.body
  }, (error, data) => {
    if (error) {
      return next(error);
      console.log(error)
    } else {
      // $push:{
      res.json(data)
      console.log('Student updated successfully !')
      // }
    }
  })
})
  • Schema structure
let studentSchema = new Schema({
  name: {
    type: String
  },
  email: {
    type: String
  },
  rollno: {
    type: Number
  },
  city: [{
    cityname: {type: String},
    citycode: {type: String},
  }],
}, 
{
    collection: 'students'
  })
  • And then in routes it is updated using $push method
// Update Student
router.route('/update-student/:id').put((req, res, next) => {
  studentSchema.findByIdAndUpdate(req.params.id, {
    $push: req.body
  }, (error, data) => {
    if (error) {
      return next(error);
      console.log(error)
    } else {
      // $push:{
      res.json(data)
      console.log('Student updated successfully !')
      // }
    }
  })
})
  • OUTPUT
[{_id: "5f12e5158be2503422bf6982", name: "sample1", email: "sample1@test.com", rollno: 1,…},…]
0: {_id: "5f12e5158be2503422bf6982", name: "sample1", email: "sample1@test.com", rollno: 1,…}
city: [{_id: "5f146fb84fb8565fb89ec33c", cityname: "city3", citycode: "12345"},…]
0: {_id: "5f146fb84fb8565fb89ec33c", cityname: "city3", citycode: "12345"}
citycode: "12345"
cityname: "city3"
_id: "5f146fb84fb8565fb89ec33c"
1: {_id: "5f15301f67c1992f4a233f77", cityname: "new city", citycode: "new code"}
citycode: "new code"
cityname: "new city"
_id: "5f15301f67c1992f4a233f77"
email: "sample1@test.com"
name: "sample1"
rollno: 1
__v: 0
_id: "5f12e5158be2503422bf6982"
Lav Sharma
  • 327
  • 1
  • 5
  • 23

2 Answers2

0

Check out This answer you might need to use the $push in your query to achieve what you want.

Hadi Mir
  • 4,497
  • 2
  • 29
  • 31
  • I have updated my post with the ```$push``` query using that. But the values are not getting saved into the database. – Lav Sharma Jul 19 '20 at 15:59
  • have you followed what he was stating in the post? he also mentioned some changes in schema, have you gone through it? thanks – Hadi Mir Jul 19 '20 at 16:01
0
  • SOLUTION
  • When the submit button is clicked. The array is stored in city as the schema is in this manner. This schema structure is there above. It is then passed to the db using axios.
onSubmit(e) {
    e.preventDefault()

    const studentObject = {
      city: [{cityname: this.state.cityname, citycode: this.state.citycode}]
    };

    axios.put('http://localhost:4000/students/update-student/' + this.props.match.params.id, studentObject)
      .then((res) => {
        console.log(res.data)
        console.log('Student successfully updated')
      }).catch((error) => {
        console.log(error)
      })

    // Redirect to Student List 
    this.props.history.push('/student-list')
  }
Lav Sharma
  • 327
  • 1
  • 5
  • 23