mongodb删除数据释放空间 mongodb库怎么增删

导读:MongoDB是一个非关系型数据库,使用起来比较灵活,可以存储大量的数据 。本文将介绍如何在MongoDB中进行增删操作 。
1. 连接到MongoDB
首先,我们需要连接到MongoDB 。使用以下代码:
```
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://:@cluster0.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
【mongodb删除数据释放空间 mongodb库怎么增删】});
这里需要替换掉``和``为你的MongoDB账号信息 。
2. 插入数据
使用以下代码向MongoDB中插入一条数据:
const insertDocuments = function(db, callback) {
// Get the documents collection
const collection = db.collection('documents');
// Insert some documents
collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
], function(err, result) {
console.log("Inserted 3 documents into the collection");
callback(result);
});
}
这里插入了三条数据 , 可以根据需要修改 。
3. 删除数据
使用以下代码从MongoDB中删除一条数据:
const deleteDocument = function(db, callback) {
// Delete document where a is 3
collection.deleteOne({ a : 3 }, function(err, result) {
console.log("Removed the document with the field a equal to 3");
这里删除了`a`等于3的那条数据,可以根据需要修改 。
总结:MongoDB是一个非常灵活的数据库,可以存储大量的数据 。在使用MongoDB时 , 我们可以通过连接到数据库、插入数据和删除数据来进行增删操作 。希望本文能够对你有所帮助 。

    推荐阅读