文档数据库服务

常用操作

2025-07-01 09:07:48

CRUD操作

在文档数据库服务中,以下是一些常用的CRUD(创建、读取、更新、删除)操作以及相应的示例:

  • 创建文档(Insert)

    • 插入单个文档:

    • db.collection.insertOne({ name: "John", age: 25, city: "New York" });
    • 插入多个文档:

    • db.collection.insertMany([
        { name: "Alice", age: 30, city: "London" },
        { name: "Bob", age: 35, city: "Paris" }
      ]);
  • 读取文档(Read):

    • 查询集合中的所有文档:

    • db.collection.find();
    • 查询并返回第一个匹配的文档:

    • db.collection.findOne({ name: "John" });
    • 使用查询操作符进行高级查询:

    • db.collection.find({ age: { $gt: 25 } });
  • 更新文档(Update):

    • 更新单个文档:

    • db.collection.updateOne({ name: "John" }, { $set: { age: 26 } });
    • 更新多个文档:

    • db.collection.updateMany({ city: "London" }, { $set: { age: 31 } });
  • 删除文档(Delete):

    • 删除单个文档:

    • db.collection.deleteOne({ name: "John" });
    • 删除多个文档:

    • db.collection.deleteMany({ city: "London" });

这些是一些常用的CRUD操作的示例。


d0Mvj.tx._NE