如何将数据附加到 MERN 堆栈中 mongodb 中的现有数组

How to append data to existing array in mongodb in MERN stack

{
  _id: 5f14675a4fb8565fb89ec338,
  name: 'sample5',
  email: 'sample5@test.com',
  rollno: 9,
  city: [
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 1',
      citycode: '000000'
    }
  ],
  __v: 0
}
city: [
    {
      _id: 5f14675a4fb8565fb89ec339,
      cityname: 'Sample City 2',
      citycode: '000002'
    }
  ],
{
  _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
}
// 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 !')
      // }
    }
  })
})
let studentSchema = new Schema({
  name: {
    type: String
  },
  email: {
    type: String
  },
  rollno: {
    type: Number
  },
  city: [{
    cityname: {type: String},
    citycode: {type: String},
  }],
}, 
{
    collection: 'students'
  })
// 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 !')
      // }
    }
  })
})
[{_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"

查看 答案,您可能需要在查询中使用 $push 来实现您想要的结果。

  • 解决方案
  • 单击提交按钮时。数组以这种方式存储在城市中。这个模式结构在上面。然后使用 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')
  }