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
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:
If you still having a difficulty, please post any error messages that you see to clarify the issue you are having.
Regards,
Wan.
--
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.
gridfs = mongoc_client_get_gridfs (client, "db", "fs", &error);file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt); assert (file);