【问题标题】:mongoclient remove collection nodejsmongoclient 删除集合 nodejs
【发布时间】:2014-07-24 16:59:41
【问题描述】:

我正在尝试删除整个集合,但它不适用于以下 nodejs 代码。使用命令 [db.collection.remove()]

在 mongodb 中同样有效
  1. 没有错误,但以下返回事件计数为 0

    smevents.count(function(err, count) {
        console.log("There are " + count + " records.");
    });
    
  2. 删除的条目从下面显示为 0-

    console.log("Removed collection entries " + collectionName + "," + removed);
    
  3. 使用的collectionName 是根据从-获得的一个-

    db.collectionNames(function(err, collections){
        console.log(collections);
    });
    

下面是代码-

function removeMongoDBCollection(db, collectionName, callback) {

    console.log('collectionname ' + collectionName);

    db.collectionNames(function(err, collections){
        console.log(collections);
    });

    db.collection(collectionName, {}, function(err, smevents) {

        console.log('Error Getting collection ' + err);

        smevents.count(function(err, count) {
            console.log("There are " + count + " records.");
        });

        smevents.remove({}, function(err, removed) {

            console.log('Error Removing collection ' + err);
            console.log("Removed collection entries " + collectionName + "," + removed);

            db.close(); 
            callback(err);
        });
    });
}

【问题讨论】:

  • 我不确定你在问什么。您能否编辑您的问题以澄清您正在尝试做什么以及什么不起作用?您是要删除集合本身还是仅删除其中的文档?

标签: node.js mongodb node-mongodb-native


【解决方案1】:

您似乎在这里缺少回调的概念,因为所有方法都是异步的,您需要等待它们完成才能继续。例如,您的“计数”方法不一定发生在“删除”之前,因为您还没有等待它完成。

async 模块可以帮助您在回调中嵌套方法时避免“缩进蠕变”。这是使用async.waterfall 对逻辑进行的一般修改,以帮助传递获得的变量。

请注意,在您的函数实现和演示流程中,当操作完成时,您通过回调内部触发“继续”到下一个“任务”:

var async = require('async'),
    mongo = require('mongodb');
    MongoClient = mongo.MongoClient;


// Function implementation
function removeCollection(db,collectionName,callback) {

  async.waterfall([

    // Get collection
    function(cb) {
      db.collection(collectionName,function(err,collection) {
        if (err) throw err;
        cb(null,collection);
      });
    },

    // Count collection current
    function(collection,cb) {
      collection.count(function(err,count) {
        console.log(
          "%s collection has %s documents",
          collectionName,
          count
        );
        cb(null,collection);
      });
    },

    // Remove collection documents
    function(collection,cb) {
      collection.remove({},function(err,removed) {
        console.log(
          "%s collection removed %s documents",
          collectionName,
          removed
        );
        cb(null,collection);
      });
    },

  ],function(err,collection) {

    collection.count(function(err,count) {
      console.log(
        "%s collection now has %s documents",
        collectionName,
        count
      );
      callback(err);
    });

  });
}

// Main flow after connection
MongoClient.connect('mongodb://localhost/test',function(err,db) {

  if (err) throw err;

  async.waterfall([

    // Set up a collection
    function(cb) {
      var collectionName = "sample";

      db.collection(collectionName,function(err,collection) {
        cb(null,collection,collectionName);
      });
    },

    // Insert some things
    function(collection,collectionName,cb) {
      async.eachSeries([1,2,3,4,5,6,7,8],function(item,complete) {
        collection.insert({ "item": item },function(err) {
          if (err) throw err;
          complete();
        });
      },function(err) {
        // When all are complete
        cb(null,collectionName);
      });
    },

    // Call your remove method
    function(collectionName,cb) {

      removeCollection(db,collectionName,function(err) {
        if (err) throw err;

        cb();
      });

    }

  ]);

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多