Promisify cursor execution: MongoDB Native Driver

2019-09-19 07:42发布

问题:

I already read up on these, but I'm having a bit of trouble executing it. mongoDB promise gets returned too early

Specifically, I'm trying to promisify the collection.find cursor, but I'm not sure I'm doing it right... any help would be appreciated:

//mongo.js

var promise = require('bluebird');
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var collection = mongodb.Collection;
promise.promisifyAll(mongodb);
promise.promisifyAll(collection.prototype);

collection.prototype._find = collection.prototype.find;
collection.prototype.find = function() {
  var cursor = this._find.apply(this, arguments);
  cursor.toArrayAsync = promise.promisify(cursor.toArray, cursor);
 return cursor;
};


var myfunction = function(callback){
  // configure and export database (async)
  mongoClient.connectAsync(database)
  .then(function(db){
    module.exports.db = db;
    promise.all([
      db.createCollectionAsync('hoth', {w:1})
        .then(function(collection){
          module.exports.hoth = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' hoth');});
        }),
      db.createCollectionAsync('endor', {w:1})
        .then(function(collection){
          module.exports.endor = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' endor');});
        }),
      db.createCollectionAsync('alderaan', {w:1})
        .then(function(collection){
          module.exports.alderaan = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' alderaan');});
        })
    ])
    .done(function(){callback(null);});
  });
};
module.exports.myfunctionAsync = promise.promisify(myfunction);

and here's app.js

// app.js
mongo.myfunctionAsync()
.then(function(result){
  mongo.hoth.mapReduceAsync(u.map, u.reduce,{
  out: {replace:'deathstar'}, scope:{size:0.01},
  query: {time:{$gte:ago}}, finalize:u.finalize});
}).then(function(deathstar){
  deathstar.findAsync({},{fields:{_id:0, value:1}});
}).then(function(docs){
  var final = _.map(docs, function(doc){return doc.value;});
}).then(function(final){
  mongo.alderaan.insertAsync(final, {w:1});
}).then(console.log('done'));

and here's the error

Cannot call method 'findAsync' of undefined

回答1:

With bluebird 2.0 you can just promisify the npm. After that cursor is promisified too.

Here is a general example with a cursor:

var MongoClient = require('bluebird').promisifyAll(require("mongodb")).MongoClient;

Then:

MongoClient.connectAsync("mongodb://localhost:27017/something")
    .then(function(db) {
        db.collection('foo').findAsync({ })
            .then(function(cursor) {       
                return cursor.toArrayAsync();
            })
           .then(function(arrayOfFoos) {
               console.log(arrayOfFoos);
           });
    });