CRUD操作
更新時間 2023-08-29 15:14:32
最近更新時間: 2023-08-29 15:14:32
分享文章
本頁介紹了文檔數據庫服務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操作的示例。