-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Native
-
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'}