2013-02-02 30 views
1

獲得的文件我有這個蒙戈 - db.js文件:蒙戈-DB錯誤,而試圖通過ID

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做出了同樣的錯誤。

爲什麼我得到這個錯誤?

+0

在您的代碼調用Object.createFromHexString(...)'時,_id的數據類型是什麼? 'console.log(typeof _id);' – WiredPrairie

+0

@WiredPrairie:這個id是我從MongoDB獲得的。首先,我加載所有項目,然後使用從mongodb獲得的ID從數據庫中獲取它。 – Naor

+0

@WiredPrairie:請參閱編輯,我把那裏的typeof結果。 – Naor

回答

0

當使用MongoClient時,返回的文檔包含BSON ObjectID值而不是字符串。一個BSON的ObjectId使用本地MongoClient具有如下的結構:

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

所以,如果你嘗試在createFromHexString方法運行它,它會崩潰,因爲它已經是一個BSON對象ID。

下面是一個簡單的例子,顯示了最初返回的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); 
      }); 
     }); 

    }); 
}); 
+0

但是如果我做id = id +「」; ,那麼是行不通的。看起來我有記錄與「空」爲_id。你知道我該如何刪除它嗎? – Naor

+0

當你執行'id = id +「」'時,它被轉換爲一個字符串。爲什麼你想從一個BSON對象ID類型轉換爲一個字符串,然後回來? – WiredPrairie

+0

文檔要求集合中包含非空ID。所以,我不確定你最終會得到一個null。如果有一些獨特的東西,那麼你應該能夠用獨特的屬性來「findOne」,然後將其刪除。 – WiredPrairie