Mongodb 2.0 nodejs driver : .find() and .count() return incompatible results

68 views
Skip to first unread message

Edgar Hipp

unread,
Jan 15, 2015, 6:08:51 AM1/15/15
to mongod...@googlegroups.com
I have a very strange issue:

My `.find()` method and `.count()` method do not return objects that are compatible, eg `.find()` returns no object at all, and `.count()` returns 1.

Here is my code (Coffeescript)

    config = require('../config.js')
    MongoClient = require('mongodb').MongoClient
   
    MongoClient.connect("mongodb://127.0.0.1:27017/#{config.General.database}",(err,db)->
   
      callback=(err,hands)->
        if err then throw err
        console.log 'find:'+hands.length
   
      callbackOne=(err,hands)->
        if err then throw err
        console.log 'findOne:'+hands
   
      db.collection('hands').find({"processed":null}).toArray(callback)
   
      db.collection('hands').findOne({"processed":null},callbackOne)
   
      db.collection('hands').count({"processed":null},(err,c)->
        console.log 'count:'+c
      )
    )

Here's a gist to the compiled JS: https://gist.github.com/27a7790580a88628aa51

This returns:

    find:0
    findOne:null
    count:1

My database consists of a huge amount of 'hands' (around 1.7 million), which are indexed with the `processed` flag.

When I run `db.hands.find()` and `db.hands.count()` in mongo command line, I get the expected result (eg the hand object, and 1).

If I roll back to version 1.4 of the nodejs mongo driver, find and count seem to work consistently.

I have also found out that if I insert another hand element, the `find` is working again.

I'm running mongod without any special options, and I am on a development machine.

Any ideas why this strange behavior ?

# Edit:

Restarting mongod didn't change anything.

Edgar Hipp

unread,
Jan 15, 2015, 8:16:19 AM1/15/15
to mongod...@googlegroups.com
Strangely if I use the {raw:true} option, the result is sent back:


config = require('../config.js')
MongoClient = require('mongodb').MongoClient
BSON = require('mongodb-core').BSON.pure().BSON;


MongoClient.connect("mongodb://127.0.0.1:27017/#{config.General.database}",(err,db)->

  callback=(err,hands)->
    if err then throw err
    console.log 'find:'+hands.length
    if hands.length>0
      # console.log hands[0].toString()
      console.log BSON.deserialize(hands[0])
    console.log hands


  callbackCount=(err,c)-> console.log 'count:'+c

  condition={"processed":null}

  db.collection('hands').count(condition,callbackCount)
  db.collection('hands').find(condition).toArray(callback)
  db.collection('hands').find(condition,{raw:true}).toArray(callback)
)
Reply all
Reply to author
Forward
0 new messages