《MongoDB入门教程》第18篇 文档更新之$unset操作符

本篇将会介绍如何利用 $unset 操作符删除文档中的字段。

$unset 操作符

$unset 是一个字段更新操作符,用于删除文档中的指定字段。

$unset 操作符的语法如下:

{ $unset: {<field>: "", ... }}

参数中的字段值不影响操作结果,可以指定任意值。如果指定字段不存在,不会执行任何操作,也不会返回错误或者警告。

如果想要指定嵌入书文档中的字段,可以使用点符号:

{ $unset: { "<embedded_doc>.<field>": "", ... }}

需要注意的是,$unset 操作符不会删除任何数组元素,而是将数组元素设置为空。

{ $unset: {"<array>.<index>": "", ...}

这种实现可以保持数组大小和元素位置的一致性。

$unset 示例

下文中的示例将会使用 products 集合:

db.products.insertMany([
    { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
    { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
    { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
    { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
    { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
 ])

示例一:删除文档中的字段

以下示例使用 $unset 操作符删除文档(_id:1)中的 price 字段:

db.products.updateOne({
    _id: 1
}, {
    $unset: {
        price: ""
    }
})

返回结果如下:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

其中,modifiedCount 表示修改了一个文档。

查询集合 products 中的全部文档:

db.products.find({}, { name: 1, price: 1 })

[
  { _id: 1, name: 'xPhone' },
  { _id: 2, name: 'xTablet', price: 899 },
  { _id: 3, name: 'SmartTablet', price: 899 },
  { _id: 4, name: 'SmartPad', price: 699 },
  { _id: 5, name: 'SmartPhone', price: 599 }
]

从返回结果可以看出,文档(_id:1)中已经没有了 price 字段。

示例二:删除嵌入式文档中的字段

以下示例使用 $unset 操作符删除集合 products 中嵌入式文档 spec 中的 ram 字段:

db.products.updateMany({}, {
    $unset: {
        "spec.ram": ""
    }
})

返回结果如下:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 5,
  modifiedCount: 5,
  upsertedCount: 0
}

查询集合 products 中的全部文档:

db.products.find({}, {
    spec: 1
})

[
  { _id: 1, spec: { screen: 6.5, cpu: 2.66 } },
  { _id: 2, spec: { screen: 9.5, cpu: 3.66 } },
  { _id: 3, spec: { screen: 9.7, cpu: 3.66 } },
  { _id: 4, spec: { screen: 9.7, cpu: 1.66 } },
  { _id: 5, spec: { screen: 5.7, cpu: 1.66 } }
]

示例三:将数组元素设置为空

接下来的示例使用 $unset 操作符将数组 storage 中的第一个元素设置为 null:

 db.products.updateMany({}, { $unset: { "storage.0": "" } })

返回结果如下:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 5,
  modifiedCount: 5,
  upsertedCount: 0
}

查看数组 storage 中的内容:

db.products.find({}, { "storage":1})

[
  { _id: 1, storage: [ null, 128, 256 ] },
  { _id: 2, storage: [ null, 256, 512 ] },
  { _id: 3, storage: [ null, 64, 128 ] },
  { _id: 4, storage: [ null, 256, 1024 ] },
  { _id: 5, storage: [ null, 256 ] }
]

示例四:删除多个字段

以下示例使用 $unset 操作符一次性删除了文档中的 releaseDate 和 spec 字段:

db.products.updateMany({}, {
    $unset: {
        releaseDate: "",
        spec: ""
    }
})

返回结果如下:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 5,
  modifiedCount: 5,
  upsertedCount: 0
}

验证更新后的文档内容:

db.products.find({}, {
    name: 1,
    storage: 1,
    releaseDate: 1,
    spec: 1
})

[
  { _id: 1, name: 'xPhone', storage: [ null, 128, 256 ] },
  { _id: 2, name: 'xTablet', storage: [ null, 256, 512 ] },
  { _id: 3, name: 'SmartTablet', storage: [ null, 64, 128 ] },
  { _id: 4, name: 'SmartPad', storage: [ null, 256, 1024 ] },
  { _id: 5, name: 'SmartPhone', storage: [ null, 256 ] }
]

查询返回的结果中没有 releaseDate 和 spec 字段。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不剪发的Tony老师

为 5 个 C 币而折腰。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值