我正在使用graphql从mongodb数据库获取一些数据。因此,我正在制作一个api,它在运行时将数据保存在主集合中,但也将数据保存在具有更多数据的其他集合中。我试图从我保存在主集合上的数据中删除_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//我对我做错了什么感到非常困惑。
编辑:对不起,应该提到的,但是根据lodash docs,如果_.unset成功删除了一个属性,它将返回true。
发布于 2020-07-20 20:05:36
Turn的out mongoDb使_id成为一个不可配置的属性。因此,为了做到这一点,我必须使用toObject()方法来创建我的对象的可编辑副本。
const NewObject = _.cloneDeep(data.record).toObject();
这样我就可以删除_id了。或者,lodash的_.omit也可以工作。
https://stackoverflow.com/questions/62994677
复制相似问题