mongo icon indicating copy to clipboard operation
mongo copied to clipboard

count doesn't work ?

Open abbysmal opened this issue 10 years ago • 10 comments

Hi, I was playing around with Mongo.ml, and when I tried to use count (to count the elements in a collection), an exception was raised and I don't understand why. Maybe I did something wrong ? Here is a sample session using utop:

─( 23:19:42 )─< command 2 >────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # #require "mongo";;
─( 23:19:50 )─< command 3 >────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # let m = Mongo.create_local_default "lamaurbain" "users";;
val m : Mongo.t = <abstr>                                                                                                                     
─( 23:19:57 )─< command 4 >────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Mongo.count m;;
Exception: Not_found.                                                                                                                         
─( 23:19:58 )─< command 5 >────────────────────────────────────────────────────────────────────────────────────────────────────{ counter: 0 }─
utop #                                                                                                                                        
kokoro:cumulus/src mongo lamaurbain                                                                                                 as engil  
MongoDB shell version: 2.6.1                                                                                                                  
connecting to: lamaurbain                                                                                                                     
> db.users.count()                                                                                                                            
7                                                                                                                                             
>

Thanks !

abbysmal avatar Feb 09 '15 00:02 abbysmal

Which version of mongo do you use ? I didn't update the repo since last year. I will try to troubleshoot it tonight if I can.

The only reason I can think of that can raise a Not Found is if MongoReply.get_document_list return a empty list, which should not happen. Do you manage to retrieve any data with other command ?

msimon avatar Feb 09 '15 02:02 msimon

I found the error. Newer version of MongoDb required parameter for count to be given in precise order. Therefore: db.$cmd.findOne({ "count": {}, "query": {}}) is correct but db.$cmd.findOne({ "query" : {}, "count": {}}) is not anymore.

I will try to fix it asap, in the meanwhile you can construct the count command yourself. The Mongo.count function is really just an helper.

msimon avatar Feb 09 '15 03:02 msimon

Thanks @msimon

MassD avatar Feb 09 '15 08:02 MassD

Thanks ! Building the count command myself seems to work, just taking the code from the mongo.ml file and swaping the first two let c_bson is enough. :)

abbysmal avatar Feb 10 '15 12:02 abbysmal

@Engil maybe you could submit a pull request?

MassD avatar Feb 10 '15 13:02 MassD

It should be okay, but I haven't tested it on MongoDB < 2.6.1. I don't think it breaks anything though.

abbysmal avatar Feb 10 '15 13:02 abbysmal

Hey @Engil thanks for contributing !

I'm not sure if the count of your pull request will work if you give him limit nor skip. But I'm certain it's working for mongo < 2.6.1 since the syntax seems to only got a bit more strict with version 2.6. If you are interested you can check: http://docs.mongodb.org/manual/reference/command/count/.

I will test all that this coming weekend and push it back to opam. Sorry I have been pretty busy with work lately.

Thanks again!

msimon avatar Feb 10 '15 14:02 msimon

Hey, also, I am not sure why you are using mongo for, but the v3 is just around the corner with awesome new functionalities but breaking change in the driver's implementation. Therefore, I'm pretty sure this driver will not work with the v3. I thought you should know.

msimon avatar Feb 10 '15 14:02 msimon

@msimon thanks for this. I am very sorry that I haven't done anything about Mongo.ml for a long time now and you are the only supporter here.

I will pick all things up from April and starting with Bson.ml and make it more like a yojson if you also think it worthy.

MassD avatar Feb 10 '15 14:02 MassD

Hey @MassD no worries, I know we are all really busy ;).

There is camlp4 syntax extension that I added for bson. It add function at compile time to add record to mongo and vice versa. Therefore I rarely use bson directly. If you check out this project https://github.com/msimon/graph_mongo, you will see that for my entire database I almost do not use bson directly.

But if you need more fine grain use of bson or do not want to use camlp4 it could be very useful.

msimon avatar Feb 11 '15 04:02 msimon