• 19.Delete Documents-官方文档摘录


    1 插入例子

    db.inventory.insertMany( [
       { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
       { 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" },
       { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
       { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
    ]);

    2 删除所有

    db.inventory.deleteMany({})

    3 删除匹配的行

    db.inventory.deleteMany({ status : "A" })

    4 删除最多一行记录

    db.inventory.deleteOne( { status: "D" } )

    5 注意

    1.删除对应元素并不会删除对应的索引
    2.删除具有原子性

    This page provides examples of delete operations using the following methods in the mongo shell:

    The examples on this page use the inventory collection. To populate the inventory collection, run the following:

    db.inventory.insertMany( [
       { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
       { 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" },
       { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
       { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
    ]);
    

    You can run the operation in the web shell below:

    Delete All Documents

    To remove all documents from a collection, pass an empty filter document {} to thedb.collection.deleteMany() method.

    The following example deletes all documents from the inventory collection:

    db.inventory.deleteMany({})
    

    The method returns a document with the status of the operation. For more information and examples, seedeleteMany().

    Delete All Documents that Match a Condition

    You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.

    To specify equality conditions, use <field>:<value> expressions in the query filter document:

    { <field1>: <value1>, ... }
    

    query filter document can use the query operators to specify conditions in the following form:

    { <field1>: { <operator1>: <value1> }, ... }
    

    To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany() method.

    The following example removes all documents from the inventory collection where the status field equals"A":

    db.inventory.deleteMany({ status : "A" })
    

    The method returns a document with the status of the operation. For more information and examples, seedeleteMany().

    Remove Only One Document that Matches a Condition

    To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the db.collection.deleteOne() method.

    The following example deletes the first document where status is "D".

    db.inventory.deleteOne( { status: "D" } )
    

    Delete Behavior

    Indexes

    Delete operations do not drop indexes, even if deleting all documents from a collection.

    Atomicity

    All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.

    Write Acknowledgement

    With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.

  • 相关阅读:
    JavaScript中的prototype
    SQL 时间格式转换
    ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
    JavaScript事件使用指南
    30行代码实现JavaScript中的MVC
    JavaScript中this指针指向的彻底理解
    JavaScript 中的命名空间
    C#嵌套类型
    C# 为枚举创建新方法
    Lambda 表达式
  • 原文地址:https://www.cnblogs.com/olinux/p/7298138.html
Copyright © 2020-2023  润新知