$ unset如何工作?

Rya*_*Mes 0 mongodb

mongo文档中,取消设置字段$unset.我不太清楚它是如何工作的,但它看起来应该很简单.

The following operation uses the $unset operator to remove the tags field:

db.books.update( { _id: 1 }, { $unset: { tags: 1 } } )
Run Code Online (Sandbox Code Playgroud)

在设定未设置的内容时会出现混乱.1$unset条款的价值是多少?

Shr*_*nee 6

根据$unset文件: -

$ unset运算符删除特定字段.

句法 : { $unset: { <field1>: "", ... } }

$ unset表达式中的指定值(即"")不会影响操作.

如果该字段不存在,则$ unset不执行任何操作(即无操作).

所以你可以使用

db.books.update( { _id: 1 }, { $unset: { tags: 1 } } )
Run Code Online (Sandbox Code Playgroud)

要么

db.books.update( { _id: 1 }, { $unset: { tags: 0 } } )
Run Code Online (Sandbox Code Playgroud)

要么

db.books.update( { _id: 1 }, { $unset: { tags: "" } } )
Run Code Online (Sandbox Code Playgroud)

以上所有查询都将删除tags字段.

希望你的疑问现在清楚.