Add a new attribute to existing json object in node.js

后端 未结 7 1920
梦如初夏
梦如初夏 2021-01-17 07:16

I have an object like this

==================records=========={ Id: 5114a3c21203e0d811000088,
  userId: \'test\',
  sUserId: test,
  userName: \'test\',
  ur         


        
7条回答
  •  自闭症患者
    2021-01-17 07:59

    My variant.

    If schema defined with {strict:false} you can simply add new property by

    recorcd.newProp = 'newvalue';

    If schema defined with {strict:true} you can either convert Mongoose object to object as mentioned earlier or use command

    record.set('newProp','newValue',{strict:false})

    See http://mongoosejs.com/docs/api.html#document_Document-schema

提交回复
热议问题