• MongoDB——文档增删改查命令使用


    MongoDB

    文档增删改查

    命令操作描述
    db.collection.insert() db.collection.insert()将单个文档或多个文档插入到集合中
    db.collection.insertOne()插入文档,3.2 版中的新功能
    db.collection.insertMany()插入多个文档,3.2 版中的新功能
    db.collection.update更新或替换与指定过滤器匹配的单个文档,或更新与指定 过 滤 器 匹 配 的 所 有 文 档 。 默 认 情 况 下 ,db.collection.update()方法更新单个文档。要更新多个文档,请使用 multi 选项。
    db.collection.updateOne(,, )即使多个文档可能与指定的过滤器匹配,最多更新与指定的过滤器匹配的单个文档。 3.2 版中的新功能
    db.collection.updateMany(,, )更新所有与指定过滤器匹配的文档。 3.2 版中的新功能
    db.collection.replaceOne(,, )即使多个文档可能与指定过滤器匹配,也最多替换一个与指定过滤器匹配的文档。
    db.collection.remove()删除单个文档或与指定过滤器匹配的所有文档
    db.collection.deleteOne()即使多个文档可能与指定过滤器匹配,也最多删除一个与指定过滤器匹配的文档。 3.2 版中的新功能
    db.collection.deleteMany()删除所有与指定过滤器匹配的文档。 3.2 版中的新功能
    db.collection.find(query,projection)查询文档
    db.collection.findOne()

    文档插入

     db.col.insert({title:'高数', description: '极限', by: '圆老师', url: 'www.gaoshu.com', tags: ['difficult', 'yue'], likes: 100})
    WriteResult({ "nInserted" : 1 })
    
    • 1
    • 2

    查看

    > db.col.find()
    { "_id" : ObjectId("5d80715e019abe974dac5164"), title:'高数', description: '极限', by: '圆老师', url: 'www.gaoshu.com', tags: ['difficult', 'yue'], likes: 100}
    
    • 1
    • 2

    更新文档

    插入

    db.inventory.insertMany( [
     { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
     { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
     { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
     { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
     { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
     { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    为了更新文档,MongoDB 提供了更新操作符(例如$set)来修改字段值。
    要使用更新运算符,请将以下形式的更新文档传递给更新方法:

    {
     <update operator>: { <field1>: <value1>, ... },
     <update operator>: { <field2>: <value2>, ... },
     ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    如果字段不存在,则某些更新操作符(例如$set)将创建该字段。
    下面的示例在 inventory 集合上使用 db.collection.updateOne()方法更新项目等于“ paper”的
    第一个文档:

    db.inventory.updateOne(
     { item: "paper" },
     { 
     	$set: { "size.uom": "cm", status: "P" },
     	$currentDate: { lastModified: true }
     }
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 使用$set 运算符将 size.uom 字段的值更新为“ cm”,将状态字段的值更新为“ P”,

    • 使用$currentDate 运算符将 lastModified 字段的值更新为当前日期。 如果 lastModified字段不存在,则$currentDate 将创建该字段。

    更新多个文档则将条件改成范围查询即可

    查询

    MongoDB 查询文档使用 find() 方法。find() 方法以非结构化的方式来显示所有文档

    db.collection.find(query, projection)
    
    • 1
    • query :可选,使用查询操作符指定查询条件
    • projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值
    db.collection.find({"item":"aa"}, {"_id":1, "status":1})
    
    • 1

    如果你需要以易读的方式来读取数据,可以使用 pretty() 方法。

    db.col.find().pretty()
    
    • 1

    指定条件查询

    db.inventory.find( {} ) //所有文档
    db.inventory.find( { status: "D" } ) //指定等于条件
    db.inventory.find( { status: { $in: [ "A", "D" ] } } )   // 或条件
    db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )  // 或条件
    db.inventory.find( { status: "A", qty: { $lt: 30 } } )  // and 条件
    db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
    } ) // and + or 条件
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    其他查询参考https://www.mongodb.com/docs/manual/tutorial/query-documents/

    删除文档

    在执行 remove()函数前先执行 find()命令来判断执行的条件是否正确,这是一个比较好的习惯。

    db.collection.remove(
    	 <query>,
    	 {
    	 justOne: <boolean>,
    	 writeConcern: <document>
    	 }
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • query :(可选)删除的文档的条件。参考查询里query写法
    • justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
    • writeConcern :(可选)抛出异常的级别。

    删除所有数据

    db.inventory.deleteMany({})
    
    • 1

    即使从集合中删除所有文档,删除操作也不会删除索引。

  • 相关阅读:
    【版本控制工具二】Git 和 Gitee 建立联系
    论文阅读——SqueezeSAM
    医院导航解决方案,医院导诊图怎么制作?哪里可以做?
    org.springframework.core.annotation.AnnotationUtils.clearCache()V 错误解决(SSM项目)
    Locust 之@task和@tag装饰器梳理
    【基本算法题-2022.7.26】3.最短Hamilton路径
    Flutter 应用程序中的 Quick Actions
    【JavaEE初阶】网络原理|认识协议|协议分层|TCP/IP模型|封装和分用
    ShowMeAI —— Show u 三连
    在学习DNS的过程中给我的启发
  • 原文地址:https://blog.csdn.net/qq_43058348/article/details/134534897