How to add data to mongodb?

there is an array cateogoryList in the defined Schema:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductCateogorySchema = new Schema({
      user:{
            type:Schema.Types.ObjectId,
            ref:"users"
      },
      cateogoryList:[
            {
                  cateogoryName:{
                        type:String,
                        required:true
                  },
                  cateogoryDes:{
                        type:String
                  }
            },
            {
                  cateogorySubList:[
                  
                  ]
            }
      ],
      date:{
            type:Date,
            default:Date.now
      }
});

module.exports = mongoose.model("cateogories",ProductCateogorySchema);

now add data to this array through the form. Normally, you have to query the Schema first, find the cateogoryList, then add the data, and then save it. But I encounter this problem here:
cannot manipulate the cateogoryList array queried into the Schema, or maybe I don"t know how to operate it, and then I can"t write the data.
on the front is the method of this POST:

router.post("/cateogory/add",urlencodedParser,(req,res) => {
      CateogoryOneSchema.find().then(cateogory => {
            if(!cateogory){
                  console.log("cateogory    :"  + cateogory)
                  const newFields = {
                        cateogoryName: req.body.cateogoryOneName,
                        cateogoryDes:req.body.cateogoryOneDes
                  }

                  const category = {};
                  category.categoryList = [];
                  category.categoryList.push(newFields);
                  cateogory.save();
            }
      })
})

the cateogory printed to is empty data that has just been inserted unsuccessfully:

but when I use category.categoryList, I will report an error that I can"t use push. What should I do?
Thank you very much!

Jun.25,2022

  1. check whether you have successfully connected to the database
  2. save is an asynchronous operation. The returned operation should be in the callback of save or in promise.then

it feels like there is something wrong with the Schema design. If you remove the array, you can add it successfully, like this:

const ProductCateogorySchema = new Schema({
      user:{
            type:Schema.Types.ObjectId,
            ref:"users"
      },
      product:[],
      cateogoryName:{
            type:String,
            required:true
      },
      cateogoryDes:{
            type:String
      },
      subTwoCateogory:[],
      subThreeCateogory:[],
      date:{
            type:Date,
            default:Date.now
      }
});
router.post('/cateogory/add',urlencodedParser,(req,res) => {
      const newFields = {
            cateogoryName: req.body.cateogoryOneName,
            cateogoryDes:req.body.cateogoryOneDes
      }
      
      new CateogoryOneSchema(newFields).save().then(cateogories => {
            res.redirect('/product/cateogory');
            res.send(cateogories);
      }).catch(err => {
            console.log(err)
      });
})
Menu