0
我剛安裝的Node.js和MongoDB的最新版本和node.js的驅動程序:MongoJSMongoJS:尺寸()方法或長度不可
現在,我試圖找出(應有不錯的表現高負載應用程序)如果數據庫中存在記錄。我嘗試這樣的查詢,但我得到的是一個錯誤:
var query = db.users.find({ nickname: 'test' }).limit(1);
console.log(query.size());
// or also
// console.log(query.length);
// => undefinied
錯誤:
TypeError: Object [object Object] has no method 'size'
通過console.log(typeof query)
我只是得到object
。
如果我只需登錄query
我得到這個:
{ _readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: false,
calledRead: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
objectMode: true,
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null }
readable: true,
domain: null,
_events: {},
_maxListeners: 10,
_get: [Function] }
我得到的所有的時間將此作爲結果,即使該項目存在與否。
你在哪裏看到的是您正在期待能夠調用'size'方法的引用? – JohnnyHK 2013-05-05 13:53:12
http://stackoverflow.com/questions/8389811/how-to-query-mongodb-to-test-if-an-item-exists – David 2013-05-05 13:56:52
打印'查詢'看起來很奇怪的原因是因爲查詢是一個遊標,它是實際上沒有任何結果。你可以通過使用findOne來解決你的問題,或者檢查query.hasNext()返回的是什麼(false - 沒有項目匹配)。我猜node.js等價於cursor.nextObject()返回null。 – 2013-05-05 17:02:57