Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1369

Cursor count() ignores collation

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Native
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Driver: mongodb@3.0.4
      Server: MongoDB 3.4.13 & 3.6.3

      The method count() on a cursor seems to ignore the collation set on the cursor.

      For example, in the shell:

      > db.test.find()
      { "_id": 0, "user": "foo" }
      { "_id": 1, "user": "foo" }
      { "_id": 2, "user": "foo" }
      { "_id": 3, "user": "Foo" }
      { "_id": 4, "user": "Foo" }
      
      > db.test.find({user:'foo'}).collation({locale:'en_US', strength:2}).count()
      5
      

      Counting on case-insensitive user: foo correctly prints 5.

      In node, both of these methods for setting the cursor's collation give the wrong count:

      MongoClient.connect('mongodb://localhost:27017/test', (err, conn) => {
          conn.db('test').collection('test').find({user:'foo'}, (err, res) => {
              res.collation({locale:'en_US', strength:2});
              res.count((err, c) => console.log(c));
              res.forEach(console.log);
              conn.close();
          });
      });
      
      MongoClient.connect('mongodb://localhost:27017/test', (err, conn) => {
          conn.db('test').collection('test').find({user:'foo'}, {collation: {locale:'en_US', strength:2}}, (err, res) => {
              res.count((err, c) => console.log(c));
              res.forEach(console.log);
              conn.close();
          });
      });
      

      Both gives the count of 3, which is only counting the lowercase foo (as per the find() parameter), although it correctly prints out the 5 documents:

      Count : 3
      { _id: 0, user: 'foo' }
      { _id: 1, user: 'foo' }
      { _id: 2, user: 'foo' }
      { _id: 3, user: 'Foo' }
      { _id: 4, user: 'Foo' }
      

      In contrast, pymongo shows the count of 5:

      db = MongoClient().test
      cur = db.test.find({'user':'foo'}).collation(Collation(locale='en_US', strength=2))
      print 'Count :', cur.count()
      for doc in cur:
          print doc
      
      Count : 5
      {u'_id': 0.0, u'user': u'foo'}
      {u'_id': 1.0, u'user': u'foo'}
      {u'_id': 2.0, u'user': u'foo'}
      {u'_id': 3.0, u'user': u'Foo'}
      {u'_id': 4.0, u'user': u'Foo'}
      

            Assignee:
            rebecca.weinberger Rebecca Weinberger (Inactive)
            Reporter:
            kevin.adistambha@mongodb.com Kevin Adistambha
            None
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: