新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

mongodb如何清空整张表数据,mongodb删除语句

发布时间:2023-11-03 04:19:23

mongodb如何清空整张表数据

在MongoDB中,可使用deleteMany()方法来清空整张表的数据。

以下是一个示例代码,展现了怎样使用deleteMany()方法清空整张表的数据:

// 连接到MongoDB数据库
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'yourDatabaseName';

MongoClient.connect(url, function(err, client) {
  if (err) throw err;

  const db = client.db(dbName);
  const collection = db.collection('yourCollectionName');

  // 删除整张表的数据
  collection.deleteMany({}, function(err, result) {
    if (err) throw err;
    
    console.log('整张表的数据已清空');
    client.close();
  });
});

注意,上述代码中的yourDatabaseNameyourCollectionName需要替换为实际的数据库和集合名称。