尝试通过id获取文档时出现mongo-db错误

时间:2013-02-02 15:38:51

标签: node.js mongodb

我有这个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(...)之前时,我会得到对象 尝试通过id=id+"";更改ID会产生同样的错误。

为什么我会收到此错误?

1 个答案:

答案 0 :(得分: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);
            });
        });

    });
});