C-Driver (v1.3.3) GridFS interface compatibility with mongoLab

71 views
Skip to first unread message

Rao Saifullah

unread,
Feb 22, 2016, 2:57:44 AM2/22/16
to mongodb-user

I am using C-driver (v1.3.3) and trying to interface mongoLab with it. Right now i am using the Sandbox and trying to interface GridFS using C-driver but i am not getting success. I can connect the C-Driver with the mongoLab and can do query but when i am trying to interface GridFS, i am not getting success. So i was wondering, can we do the GridFs interface with mongolab or not? I read FAQs and found out that it's possible but then any hints or proposed solution. Here is my code

const char* uriString = "mongodb://<user>:<pwd>@ds012345.mongolab.com:12345/tempdb";
uri = mongoc_uri_new(uriString);
client = mongoc_client_new_from_uri (uri);
database = mongoc_client_get_database (client, "tempdb");

    if ((strv = mongoc_database_get_collection_names (database, &error))) {
        for (i = 0; strv [i]; i++)
            printf ("%s\n", strv [i]);
        bson_strfreev (strv);
    } else {
        fprintf (stderr, "Command failed: %s\n",error.message);
    }

gridfs = mongoc_client_get_gridfs (client, "tempdb","fs", &error);

       bson_init (&query);
    bson_append_document_begin (&query, "$orderby", -1, &child);
    bson_append_int32 (&child, "filename", -1, 1);
    bson_append_document_end (&query, &child);
    bson_append_document_begin (&query, "$query", -1, &child);
    bson_append_document_end (&query, &child);

    list = mongoc_gridfs_find (gridfs, &query);
    bson_destroy (&query);

    while ((file = mongoc_gridfs_file_list_next (list))) {
        const char * name = mongoc_gridfs_file_get_filename(file);
        printf("%s\n", name ? name : "?");

        mongoc_gridfs_file_destroy (file);
        count ++;
    }

mongoc_gridfs_file_list_destroy (list);
mongoc_stream_destroy (stream);
mongoc_gridfs_file_destroy (file);

Looking forward to a positive response.

Thanks

Wan Bachtiar

unread,
Feb 25, 2016, 7:26:34 AM2/25/16
to mongodb-user

I can connect the C-Driver with the mongoLab and can do query but when i am trying to interface GridFS, i am not getting success

Hi Rao,

Could you please elaborate on what you mean by “not getting success” ? Are you getting a connection error, or maybe segmentation fault ?

I have just tested the gridfs functionality using mongo-c-driver v1.3.3 with MongoLab. I used MongoLab sandbox MongoDB v3.0.9, using the code example-gridfs.c which read, write and list gridfs files. All of the mentioned operations work in the test without any issue.

Afterward I also tested your snippet code, with a bit of modification to list gridfs files:

client = mongoc_client_new ("mongodb://<user>:<password>@<hostid>.mlab.com:<port>/dbname");
gridfs = mongoc_client_get_gridfs (client, "dbname","fs", &error);
assert(gridfs);

bson_init (&query);
bson_append_document_begin (&query, "$orderby", -1, &child);
bson_append_int32 (&child, "filename", -1, 1);
bson_append_document_end (&query, &child);
bson_append_document_begin (&query, "$query", -1, &child);
bson_append_document_end (&query, &child);

list = mongoc_gridfs_find (gridfs, &query);
bson_destroy (&query);

while ((file = mongoc_gridfs_file_list_next (list))) {
    const char * name = mongoc_gridfs_file_get_filename(file);
    printf("%s\n", name ? name : "?"
);
    mongoc_gridfs_file_destroy (file);
}
mongoc_gridfs_file_list_destroy (list);
mongoc_gridfs_destroy (gridfs);
mongoc_client_destroy (client);
mongoc_cleanup ();

Which works as well. You should be seeing something similar to:

2016/02/25 23:12:43.0382: [20381]:     INFO:      cluster: SCRAM: authenticating "user" (step 1)
2016/02/25 23:12:43.0705: [20381]:     INFO:      cluster: SCRAM: authenticating "user" (step 2)
2016/02/25 23:12:43.0898: [20381]:     INFO:      cluster: SCRAM: authenticating "user" (step 3)
2016/02/25 23:12:44.0098: [20381]:     INFO:      cluster: SCRAM: "user" authenticated
grid_file_001
...

You may want to confirm:

  • Your MongoLab username, password and host. Try connecting via mongo shell first to start with to make sure the URI is valid.
  • Your database name. Make sure you have the same database name in MongoLab and in your code.

If you still having a difficulty, please post any error messages that you see to clarify the issue you are having.

Regards,

Wan.

Rao Saifullah

unread,
Mar 16, 2016, 5:32:29 AM3/16/16
to mongod...@googlegroups.com
Hi Wan,

Thanks for reply. I actually solved the error before you posted the solution here. The problem was that i was using very old C-Driver for mongoDB. Now it's working perfectly. Thanks

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/ahsl1zFQnhQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/595c9a05-6f95-4d8c-b325-f3cb53eaf8c0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rao Saifullah

unread,
Feb 6, 2017, 9:58:01 PM2/6/17
to mongodb-user
Hi Wan,

Did monoglab recently changed any security stuff, because again  i am getting error when i am querying on GridFS. My code is simple but i can not get the handle for gridFS with this command.

gridfs = mongoc_client_get_gridfs (client, "db", "fs", &error);

and when i am trying to do anything further i get the error that precondition failed. 

file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt);
      assert (file);

At this line, i get the error. Even though i can successfully query the database and can check the collections but working with GridFs is not possible. I tried to use "list" command but i am getting precondition failed. 

Any hint?
Reply all
Reply to author
Forward
0 new messages