使用 mongoose 将 mongo 对象的字段设置为空 [英] set field as empty for mongo object using mongoose
问题描述
我在一个对象上调用 user.save(),在那里我设置了 user.signup_date = null;
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屋!