0

Official documentation seems to lack step by step instructions for installing and usage of c++ driver. This thread and the answer seem more promissing. But still I'm not sure what I'm doing wrong, because I can not $ locate client/dbclient.h in Ubuntu, as if mongo driver was not installed. So, this is what I did:

  1. Dowloaded driver at this link -ok
  2. Unzipped the archive - ok
  3. installed scons like so $ sudo apt-get install scons
  4. Did $ scons and then
  5. sudo $ scons install
  6. opened /etc/ld.so.conf file and added /usr/local/lib

After all these steps I still can not locate client/dbclient.h in my system. It seems as if I missed something. Besides, when I get to the stage when the library gets installed, I want to read the core of the library, because in tiny tutorials on the Web I can not find everything I want (I guess because of the lack of full-fledged tutorials similar to those of Python). So, can you also, please, advise what library files I should look at to learn the api of functions, that I will be using?

EDIT

And this official link seems to be outdated, because $ scons --full install results in error message No such option: --full. The lack of tutorials, outdated documentation and rather steep learning curve, IMHO, make usage of c++ rather problematic.

EDIT

Well, it seems like this doc is not just outdated, but incorrect, since it sdould be:

scons --prefix=/usr/include install.

However, even though I specify --prefix=/usr/include I can not locate any mongo files inside this folder after installation process. I face exactly the very same problem as a guy in this thread. I guess, my final question should be "Does anybody in the world use mongodb in C++?"

EDIT

If I try to do

$ sudo git clone git@github.com:mongodb/mongo-cxx-driver.git

Then I get Could not read from remote repository etc. It seems like they did everything to prevent using mongdb in c++.

Community
  • 1
  • 1
Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • The answer by Petesh on the question you linked suggest there are packages in the Ubuntu repositories. Don't the packages `mongodb-dev` and `libmongo-client-dev` contain what you want ? – Leiaz Jul 24 '15 at 13:13
  • 1
    `sudo apt-get install mongodb-dev` => error message `Unable to locate package`. It seems like all advises prior to January 2015 are outdated and there is a need to make a brand new step by step tutorial of installing and using mongodb driver in Ubuntu. – Jacobian Jul 24 '15 at 13:27
  • I wish someone from mongodb developers could provide such a tutorial. – Jacobian Jul 24 '15 at 13:29

2 Answers2

2

EDIT: This posting gets a lot of traffic, but describes how to build the now-officially-out-of-date "Legacy C++" Driver. The Legacy C++ driver has been obsoleted by the new C++11 driver. The new C++11 driver should be the first choice when writing a client application in C++ that will communicate with MongoDB. Information on how to build the new C++11 driver is available here:

https://github.com/mongodb/mongo-cxx-driver/wiki/Quickstart-Guide-(New-Driver)

Modern instructions for building the driver can be found on the GitHub Wiki page for the C++ driver. Please see the following:

https://github.com/mongodb/mongo-cxx-driver/wiki/Download-and-Compile-the-Legacy-Driver

https://github.com/mongodb/mongo-cxx-driver/wiki/Tutorial

acm
  • 12,183
  • 5
  • 39
  • 68
  • 1
    I also got the answer at this thread - http://askubuntu.com/questions/652273/unable-to-locate-package-mongodb-dev . I just downloaded .deb package from http://launchpadlibrarian.net/170834766/mongodb-dev_2.4.9-1ubuntu2_i386.deb and did all the other stuff – Jacobian Jul 25 '15 at 11:08
  • Can you, please, advise me where I can read about the api of functions. For example, it is not enough for me to simply call `mongo::DBClientConnection c; c.connect("localhost"); ` Because I wish to define port number, user login and user password. From this page I only understand how to specify port - just like c.connect("127.0.0.1:8080"); But what about other parameters? I can not see anything about user and password. Should I do it like user:password@127.0.0.1:8080 or should I do it another way? Can you, please, give a reference to the documentation where I can read about it? – Jacobian Jul 25 '15 at 12:13
  • I meant this page of documentation - http://api.mongodb.org/cxx/current/classmongo_1_1_d_b_client_connection.html#a2d03f11e5355d3b4b3e0b0d28f2a21ab – Jacobian Jul 25 '15 at 12:14
  • I and besides I do not get from the documentation how to connect to a particular database. Unfortunately, in all tutorials and all examples I see on the web it is done like `c.connect("localhost");` and I can not find the way how I can connect to a particular database. I'm unable to digest `const std::string & dbname` construct - what does it mean and how it can be used int the context of a real example. – Jacobian Jul 25 '15 at 12:34
  • 1
    Please do not use that .deb. It is from an ancient version of the driver, from before it was separated from the server and improved. You want to use the legacy-1.0.3 driver built from the github sources. – acm Jul 25 '15 at 14:18
  • 1
    For your question about how to connect (only with the legacy driver), please see the following for how to use MongoDB URI syntax and ConnectionString::parse to connect: https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/client/examples/tutorial.cpp#L91-L105 – acm Jul 25 '15 at 14:21
  • BTW. The tutorial you pointed at is different from this one - https://github.com/mongodb/mongo-cxx-driver/wiki/Tutorial I wonder if this last one is up to date. – Jacobian Jul 25 '15 at 16:36
  • You are right. What I meant to say, that the way how connection is set in this tutorial - https://github.com/mongodb/mongo-cxx-driver/blob/legacy/src/mongo/client/examples/tutorial.cpp is different from the way how it is done in this tutorial - https://github.com/mongodb/mongo-cxx-driver/wiki/Tutorial. But I checked it and both methods seem to works. So, it does not matter any more. Thank you, again! – Jacobian Jul 25 '15 at 16:55
  • Ah, I see. Yes we should probably update the tutorial to use connection URLs, which are preferred. – acm Jul 25 '15 at 17:29
  • Although, this method with `DBClientConnection c; c.connect("localhost");` seems more straightforward and clear, especially if we compare it to the way how mongodb client is implemented in other languages, like Python etc. But of course it is just my personal opinion. Probably in c++ world they tend to do things in a more complex way. – Jacobian Jul 25 '15 at 18:36
  • It's sad to see that there is no deb File with the most current version, it makes me avoid MongoDB, seems that the devs forgot that other devs need to use their connectors with ease not with difficulty. – Oliver Oct 09 '16 at 01:20
1

A note from a year on: many of the links above are now obsolete and point forwards to these github pages. That page is more generic than the responders above seem to have intended. There is then an onwards pointer to this tutorial, which is good but does make a few assumptions that your development environment is more or less like the mongo developers'.

It's not as smooth sailing as one might like, even on ubuntu 16.04. Read it all with the prefix "Here be dragons" for now.

A couple notable issues:

  • The tutorial points to a specific file to fetch via curl rather than pointing to the github repository and suggesting doing a clone. This is a recipe for being a bit out of date. If I tell my infrastructure to fetch release 3.0.1, it will happily do that forever, rather than updating. That said, it appears that compiling from git is fine.

  • A prerequisite is installing libmongoc. It sure looks like installing from standard ubuntu packages is enough, since that's what the referenced page says. But the mongocxx installer wants >=1.3.4. So installing from git is required.

  • It depends on libbson >= 1.3.4, which is later than the ubuntu 16.04 installed version. So installing that, too, from git is required.

  • Note that none of the git packages specify which branch or tag to request in order to get a production build. Looking at all the tags on master and picking the numerically largest with no "rc" in the name seems to work. A tag called "release" or "current" would be oh-so-kind.

  • The build (typing make) tries to do a make install of the subpackage EP_mnmlstc_core, and this fails. So the entire build apparently has to run as root.

  • At first pkg-config didn't work for me on mongocxx. Then it did. I assume I made some subtle typo that I can't see looking back, but I still don't see it. Alternatively, something happened since that got it working.

My compile line then is this:

g++ --std=c++11 c++-hello-world-v3.cc -o hw   \
    $(pkg-config --cflags --libs libmongocxx) \
    -Xlinker -rpath /usr/local/lib

I would really like to file a documentation issue with all of this on mongo's github repo. Unfortunately, they have disabled the issue tracker, instead point me to their jira instance, and there I appear not to have the right to create issues. (Sometimes I miss what's in front of my nose, though.) [Update: David Golden kindly entered the bug info and noted how I might have as well.]

jma
  • 3,580
  • 6
  • 40
  • 60
  • Thank you for the detailed feedback. Lets start with the linking issue. Do you, perhaps, also have libmongoc and libbson installed from packages? Are the copies in /usr/local/lib the only ones on your system? If not, is it possible that since you have not specified a lib path that you are not picking up the libbson from /usr/local, but another one? Can you try adding -L/usr/local/lib to your link line? Other things to consider would be uninstalling the system libmongoc and libbson. Finally, the best way to express the correct link flags is to use pkg-config. Why didn't that work for you? – acm Oct 06 '16 at 14:55
  • FYI: I've filed your notes as [CXX-1070](https://jira.mongodb.org/browse/CXX-1070) as a reminder to address these concerns in the docs. – xdg Oct 06 '16 at 15:28
  • I must have made a very subtle typo yesterday with pkg-config. It is working correctly today. And the -L also solved the linker errors (as does switching to using pkg-config). Running (or ldd) says no: libmongocxx.so._noabi, libbsoncxx.so._noabi not found. Aha, a hack that works is setting LD_LIBRARY_PATH. (Ick. ;-) – jma Oct 06 '16 at 16:24
  • Just to make sure I'm clear on where you are: You have now linked successfully, however, at runtime, the program fails stating that the dynamic libraries for mongocxx and bsoncxx are not found? Most likely, you need to add /usr/local/lib to the rpath for your program. It seems that pkg-config does not actually do this, unfortunately. You can test this by seeing if setting LD_LIBRARY_PATH=/usr/local/lib in your shell environment allows your program to run. There may be improvements that can be made on our side to improve this. – acm Oct 06 '16 at 16:28
  • Another doc bug (maybe) is the prerequisite "A mongod instance running on localhost on port 27017." Perhaps not any mongod instance is good enough. I have one that works fine, but trying to access it via test fails: "terminate called after throwing an instance of 'mongocxx::v_noabi::bulk_write_exception' what(): No suitable servers found (`serverSelectionTryOnce` set): [Server closed connection. calling ismaster on 'localhost:27017']: generic server error Aborted (core dumped)" – jma Oct 06 '16 at 16:34
  • @acm - correct, exactly. And I had just edited my response to note that LD_LIBRARY_PATH works. Hunting for g++'s equivalent of -R / -rpath now... – jma Oct 06 '16 at 16:36
  • Aha, -Xlinker -rpath /usr/local/lib – jma Oct 06 '16 at 16:40
  • Yeah, I think you cal also write it as -Wl,-rpath,/usr/local/lib. – acm Oct 06 '16 at 16:43
  • I think it is interesting to understand why pkg-config doesn't do that. Perhaps our .pc file should be specifying rpath in some situations? I've filed https://jira.mongodb.org/browse/CXX-1071 for us to follow up. – acm Oct 06 '16 at 16:45
  • Does your local server have auth enabled? – xdg Oct 07 '16 at 10:08
  • Some comments on your bullet points. 1) Agreed we could do this better. 2 & 3) I'd love it if Ubuntu would update to 1.3.latest. It is a patch release so they should be willing. An upstream bug from a user will carry more weight. Could you file one and let us know? 4) Good idea, filed as https://jira.mongodb.org/browse/CXX-1074. 5) Note that you can also build the driver against system MNMLSTC, or boost, or std::experimental, none of which require this step. We may also vendor MNMLSTC in the future. 6) pkg-config is fun! – acm Oct 07 '16 at 12:56
  • @acm can you not make a proper .deb package? I've used MongoDB from the start, when it works it is a joy, library updating is a horror though. The "Border to entry" is so huge compared to any other database available. Devs are wasting more time integrating the library then writing code for it. (Even a zip file with the header files and the static library would be an improvement or a script which pulls through completely) – Oliver Oct 09 '16 at 01:27
  • @OliverStutz - Our end goal for the new C++11 driver is to get it upstreamed into Debian, as we have done for the C driver, for exactly the reason you point out. We are actively working towards that goal. – acm Oct 09 '16 at 17:36
  • @xdg Yes, I do have auth enabled. Check out [here](https://groups.google.com/forum/#!topic/mongodb-user/4wXRQj9U-nM) for (ongoing) follow on to the auth issue. – jma Oct 10 '16 at 12:06
  • Ubuntu bug [here](https://bugs.launchpad.net/ubuntu/+source/libmongo-client/+bug/1631933) for those who would like to weigh in. – jma Oct 10 '16 at 12:13
  • @jma Thanks for filing that. I've forwarded it to the mongoc developers. – acm Oct 10 '16 at 13:45