简体   繁体   中英

How to install and use mongodb c++ driver in Ubuntu?

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++.

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

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.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM