admin管理员组文章数量:1302929
My Modal :
{
"name":{type:String,required:true},
"category":{type:mongoose.Schema.Types.ObjectId,ref:"Category"}
}
I have a document created using this modal and the document looks like:
{
"_id":ObjectId("5dsfkjh2r74dsjdhf3r4f"),
"name":"demo 1",
"category":ObjectId("5ae9dlkj32nds6n37cj23")
}
If I try to change the category field, for eg:
document.category = ''
document.category = null
document.category = undefined
I'm getting the following error:
Cast toObjectId failed ...
I need to unset the "category"
field to null
or empty
or even delete it. How to do that?
My Modal :
{
"name":{type:String,required:true},
"category":{type:mongoose.Schema.Types.ObjectId,ref:"Category"}
}
I have a document created using this modal and the document looks like:
{
"_id":ObjectId("5dsfkjh2r74dsjdhf3r4f"),
"name":"demo 1",
"category":ObjectId("5ae9dlkj32nds6n37cj23")
}
If I try to change the category field, for eg:
document.category = ''
document.category = null
document.category = undefined
I'm getting the following error:
Cast toObjectId failed ...
I need to unset the "category"
field to null
or empty
or even delete it. How to do that?
3 Answers
Reset to default 5Try unsetting
the document
document.update({_id: "5dsfkjh2r74dsjdhf3r4f"}, {$unset: {category: 1 }});
or
document.update({_id: ObjectId("5dsfkjh2r74dsjdhf3r4f")}, {$unset: {category: 1 }});
ObjectId
should be a 24 length string which I guess your id
is not. That's why mongo cannot convert it into valid ObjectId
.
One solution would be to generate 24 character valid ObjectId
for category field.
Other would be to set category
as a simple String
instead of ObjectId
Use Below line document.category = new MongoDB\BSON\ObjectID('');
本文标签: javascripthow to set mongo objectId field to null or empty or even delete the fieldStack Overflow
版权声明:本文标题:javascript - how to set mongo objectId field to null or empty or even delete the field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741743398a2395390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论