Node.js 从来自mongodb的对象中删除"u id"不会';行不通

Node.js 从来自mongodb的对象中删除"u id"不会';行不通,node.js,mongodb,mongoose,graphql-compose,Node.js,Mongodb,Mongoose,Graphql Compose,我正在使用graphql从mongodb数据库获取一些数据。所以,我制作了一个api,它在运行时会将数据保存在主集合中,但也会将数据保存在其他集合中,并包含更多的数据。我试图从我在主收藏中保存的数据中删除\u id,但它不起作用,我不知道为什么 下面是发生的事情 const data = await User.getResolver('updateById').resolve({ //default resolver from graphql-compose library to updat

我正在使用graphql从mongodb数据库获取一些数据。所以,我制作了一个api,它在运行时会将数据保存在主集合中,但也会将数据保存在其他集合中,并包含更多的数据。我试图从我在主收藏中保存的数据中删除
\u id
,但它不起作用,我不知道为什么

下面是发生的事情

  const data = await User.getResolver('updateById').resolve({ //default resolver from graphql-compose library to update a record. Returns the whole document in record //
    args: {...rp.args}
  })
  const version = '4'
  const NewObject = _.cloneDeep(data.record);
  NewObject.version = version;
  NewObject.oldId = data._id;
  _.unset(NewObject, '_id'); //this doesn't work neither does delete NewObject._id//
  console.log('new obj is', NewObject, version, NewObject._id,  _.unset(NewObject, '_id')); //prints _id and surprisingly the unset in console.log is returning true that means it is successfully deleting the property//
我对自己做错了什么感到很困惑


编辑:抱歉,应该提到,但是根据
如果成功删除属性,unset将返回
true

结果mongoDb将
\u id
作为不可配置属性。因此,要做到这一点,我必须使用
toObject()
方法来制作对象的可编辑副本

const NewObject=u.cloneDeep(data.record.toObject()

有了这个,我可以删除
\u id

lodash的另一种选择也有效。

Turn's out mongoDb将
\u id
作为不可配置的属性。因此,要做到这一点,我必须使用
toObject()
方法来制作对象的可编辑副本

const NewObject=u.cloneDeep(data.record.toObject()

有了这个,我可以删除
\u id
。 另一种方法是使用lodash