使用mongoose将mongo对象的字段设置为空 [英] set field as empty for mongo object using mongoose

查看:675
本文介绍了使用mongoose将mongo对象的字段设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置user.signup_date = null;的对象上调用user.save();

I'm calling user.save() on an object, where I set user.signup_date = null;

user.first_name = null;
user.signup_date = null;

user.save();

但是当我在mongodb中查看用户时,仍然设置了signup_date和first_name ...如何有效地将此字段设置为空或空?

But when I look at the user in the mongodb it still has the signup_date and first_name set...how do I effectively set this field as empty or null?

推荐答案

要从现有文档中删除这些属性,请将其设置为undefined而不是null,然后再保存文档:

To remove those properties from your existing doc, set them to undefined instead of null before saving the doc:

user.first_name = undefined;
user.signup_date = undefined;

user.save();

确认在Mongoose 5.9.7中仍可使用.请注意,您仍要在架构中定义要删除的字段,这样才能正常工作.

Confirmed as still working in Mongoose 5.9.7. Note that the field you're trying to remove must still be defined in your schema for this to work.

这篇关于使用mongoose将mongo对象的字段设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆