1

我有这个 mongo-db.js 文件:

var MongoClient = require('mongodb').MongoClient,
    ObjectID = require('mongodb').ObjectID;

exports.MongoDB = function() {
    this.DB_NAME = "myDBNAME";
    this.MongoClient = MongoClient;

    return this;
};

exports.MongoDB.prototype.openDB = function(action) {
    console.log("Open DB");
    var scope = this;
    this.MongoClient.connect(this.generateMongoUrl(), function(err, db) {
        if (!err) {
            console.log("Open DB Success");
            if (action && typeof action === "function") {
                action(db, scope);
            }
        } else {
            console.log("DB Connect Error: " + err);
        }
    });
};

exports.MongoDB.prototype.closeDB = function(db, action) {
    var scope = this;
    return function() {
        console.log("Close DB and apply action with arguments");
        db.close();
        action.apply(this, arguments);
    };
};

exports.MongoDB.prototype.getItemById = function(id, callback) {
    this.openDB(function(db, scope) {
        console.log("Retrieving item: " + id);
        db.collection("items", function(err, collection) {
            if (err) { callback(err); }
            else {
                collection.findOne({ _id: ObjectID.createFromHexString(id) }, scope.closeDB(db, callback));
            }
        });
    });
};

exports.MongoDB.prototype.getAllItems = function(callback) {
    this.openDB(function(db, scope) {
        console.log("Retrieving all items");
        db.collection("items", function(err, collection) {
            if (err) { callback(err); }
            else {
                collection.find({ }).toArray(scope.closeDB(db, callback));
            }
        });
    });
};

我运行以下命令:

var scope = this;
var _items = [];
var counter = 0;

var done = function() {
    console.log("done!");
};

var getItem = function(error, item) {
    if (error) { }
    else {
        console.log("done loading item " + item._id);
        _items.push(item);
        if (_items.length === counter) {
            done();
        }
    }
};

this.db.getAllItems(function(error, items) {
    if (error) { }
    else {
        for (var i = 0; i < items.length; i++) {
            var id = items[i]._id;
            if (id) {
                counter++;
                console.log("load item " + id);
                scope.db.getItemById(id, getItem);
            }
        }
    }
});

运行此代码时,我得到:

Open DB
Open DB Success
Retrieving all items
Close DB and apply action with arguments
load item 50fb26263d47b70000000001
Open DB
load item 50fb277f172a5d0000000001
Open DB
load item 50fb2aa7865d870000000001
Open DB
load item 5102b7ddfe581ce5c7000001
Open DB
load item 5109678839aefde9fe000001
Open DB
load item 51096a91d0b50572ff000001
Open DB
load item 51096b06405d398bff000001
Open DB
load item null
Open DB
load item 51098b6b58bc1d0000000001
Open DB
load item 51098e16fb0e710000000001
Open DB
load item 51098e31a725100000000001
Open DB
load item 510991f20bc7690000000001
Open DB
load item 51099261258c710000000001
Open DB
load item 5109928b7edf7a0000000001
Open DB
load item 510992f0c73ccc0000000001
Open DB
load item 51099336e8a0090000000001
Open DB
load item 5109938d5fc9ce0000000001
Open DB
load item 510993cc8159610000000001
Open DB
load item 51099530ab74fb0000000001
Open DB
load item 5109956e8b059e0000000001
Open DB
load item 510995965f38da0000000001
Open DB
load item 510995ca14c0610000000001
Open DB
load item 5109963f2acd750000000001
Open DB
load item 5109966fc7001b0000000001
Open DB
Open DB Success
Retrieving item: 5109928b7edf7a0000000001

/myproj/node_modules/mongodb/lib/mongodb/connection/server.js:481
        throw err;
              ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at Function.createFromHexString (/myproj/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:212:11)
    at exports.MongoDB.getItemById (/myproj/js/mongo-db.js:95:52)
    at Db.collection (/myproj/node_modules/mongodb/lib/mongodb/db.js:496:44)
    at exports.MongoDB.getItemById (/myproj/js/mongo-db.js:92:12)
    at exports.MongoDB.openDB (/myproj/js/mongo-db.js:72:17)
    at MongoClient.connect (/myproj/node_modules/mongodb/lib/mongodb/mongo_client.js:112:5)
    at _finishConnecting (/myproj/node_modules/mongodb/lib/mongodb/db.js:2126:7)
    at Db.open (/myproj/node_modules/mongodb/lib/mongodb/db.js:249:14)
    at Server.connect.connectCallback (/myproj/node_modules/mongodb/lib/mongodb/connection/server.js:277:7)
    at g (events.js:192:14)

当我放console.log(typeof id);之前Object.createFromHexString(...),我得到对象。
尝试通过这样做来更改 idid=id+"";犯了同样的错误。

为什么我会收到此错误?

4

1 回答 1

0

使用 MongoClient 时,返回的文档包含 BSON ObjectID 值而不是字符串。使用本机 MongoClient 的 BSON ObjectId 具有如下结构:

_id = {
    id : "{12 bytes of binary data}",
    _bsontype : "ObjectID"
}

所以,如果你尝试在 createFromHexString 方法上运行它,它会崩溃,因为它已经是一个 BSON ObjectID。

这是一个简单的示例,展示了如何不需要从最初返回的 Id 进行转换。

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

MongoClient.connect("mongodb://localhost:27017/default?w=1", function (err, db) {
    db.collection("research").find({}, {'_id' : 1}).toArray(function(err, results) {
        results.forEach(function(doc) {
            console.log(doc._id + " type " + typeof doc._id);
            // now async get all docs            
            db.collection("research").findOne({'_id': doc._id}, function(err, doc) {
                console.log("# " + doc._id);
                console.log(doc);
            });
        });

    });
});
于 2013-02-02T18:17:41.190 回答