mongodb
installation
In linux debian, it does have an install candidate : V2.4 which works.
Note that v 2.4 cannot handle 3D geo coordinate search, 3.4 and up do. Stretch has version 3, jessie does NOT.
If switching to mongodb-org, the legacy driver will be removed by apt-get. You will need to manually compile libmongocxx to compile c++
I will presume you want to install the latest version, in that case you need an extra apt.source.
If you are upgrading to 3.6 from 2.X and also have data in you mongodb, you need to install 3.4 first. see upgrade below.
Otherwise i presume the latest, which is 3.6 at the time of writing. <https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/>_
| changetitle | |
|---|---|
If no errors, the mongodb daemon should be running, try to connect with the command line tool mongo :
| changetitle | |
|---|---|
upgrade
It is best to install 3.4 if you have data in you database : <https://docs.mongodb.com/v3.4/tutorial/install-mongodb-on-debian/>_
Probably the data will be readable by 3.6 if you started the database under 3.4 . I stuck with 3.4 and have not tried 3.6 after that.
quick start
Now try out the web interface at port 28017, http://localhost:28017/ A very terse set of instructions :
| changetitle | |
|---|---|
overview
As database is created when you use it so :
Yields:
Closest object to a collection in mysql would be table, but of course it has no table structure, because mongodb enforces no structure to documents.
If you did the use bag commands before, you will probably get nothing when showing the collections :
| changetitle | |
|---|---|
Again, collections are created when used, so read 'documents' below first or just do :
These are 2 documents, insert them into (and create along the way) the collection coll
Yes, the db is mandatory, and it is not the name of the database, just always "db". After this you will have a collection:
| changetitle | |
|---|---|
system.indexes was not there before either, it contains the indexes like :
| changetitle | |
|---|---|
Gives :
| changetitle | |
|---|---|
And the command :
| changetitle | |
|---|---|
will show you your coll collection:
| changetitle | |
|---|---|
I inserted k twice, and you see that it get's a different id, but is the same for the remainder of the document.
this will delete the contents of the collection, but it's still visible with show collections
will wipe that entry as well. Document Closest object in mysql would be a table row.How to create is already shown above, but in detail it is a free form json document and it will get a generated unique id when inserting it into a collection.
Actually... it is not json but [bson]
import and export To get bulk data into mongodb, these commands come in handy. Let's start with mongoexport, because that will give a file with the correct format to import again. It will give something like this (not unexpectedly ;) :<pre>
{ "_id" : { "$oid" : "51ed584d395b6f70b21b68a9" }, "name" : "mongo" }
{ "_id" : { "$oid" : "51ed5850395b6f70b21b68aa" }, "x" : 3 }
{ "_id" : { "$oid" : "51ed5851395b6f70b21b68ab" }, "x" : 3 }
</pre>
My next guess was almost correct, but the file parameter is --file, not --in so :
searching
mongodb uses regular expressions for this. Some examples :
Gives tries to return all records, but it's limited to the first x (?) records. The x seems to be a screen full , you can see it's not all records by the closing statement "has more".You can limit it explicitly, but it will still not show more than it's own limit:
You can also apply a direct search, note that the key can be without quotes but the value was entered as a string and the quotes are needed. Result :<pre>
{ "_id" : ObjectId("52028c76483cd6bd4c5a2e64"), "id" : "1883300000000160", "name" : "Biesenhof", "type" : "Weg", "city_id" : "2819" }
{ "_id" : ObjectId("52028c76483cd6bd4c5a2e65"), "id" : "1883300000000160", "name" : "Biesenhof", "type" : "Weg", "city_id" : "3512" }
</pre>
wildcards
You can use regular expressions to do some fuzzy search :
| changetitle | |
|---|---|
Also to search for a value case-insensitive, use something like :
| changetitle | |
|---|---|
Though keep in mind it will be rather slow, for full speed convert everything (data and search) to one case, either upper or down.
indexes
Or is it indices , don't care...
I take an example of the bag database, in which all postal codes with all numbers occupy one document in the database so :
Me and the neighbors are bot one 'row' in the database. A query on this with a bogus postal code (lower case 3151aw was enough already) took 6 seconds. That's not why I chose mongodb, it should be able to cope with shit loads of data. But here is how to add an index :creating indexes
The 1 means ascending order (s you can probably guess what -1 will do 8)The same find operation :
Now returns instantly although it could not find anything. Do we need another index on number ? Well this also return directly (WITH a result) :index info
Showing your index can be done with :
| changetitle | |
|---|---|
Result :
| changetitle | |
|---|---|
As you can see, there is another index on "_id" (it is default for all collections).
index deletion
Deleting with :
geographical indexes
First of all, this will not work in the debian default mongo installation (2.0.6) you will need at least version 2.6. You can install it by adding this to your repository :
| changetitle | |
|---|---|
And then install mongodb-org
Now you're not done of course, because the client libs will not work anymore. Sigh... A newer version of these are at mongodb.org But it needs to be compiled from source. However that can't be called a hard job, but it takes some time :
| changetitle | |
|---|---|
Now this hideous compile line is needed to get things both compiled and installed :
| changetitle | |
|---|---|
Why is beyond me, for instance the install-mongoclient target will not be recognized without the --full flag, and more of that nonsense ... just do it.
To create a geographical index, use
| changetitle | |
|---|---|
To delete :
| changetitle | |
|---|---|
troubleshooting
recovery
I had a problem starting up after a hard crash, so you can't really blame mongodb, however read this : https://www.quora.com/What-is-the-proper-way-to-handle-MongoDB-file-corruption. At least that states that postgres is still more robust and that you should not use mongodb for everything, only BIG data.
But this recovery was quite straightforward :
- mongodb uses a journal that can be used to recover after a crash
- this is default on in /etc/mongod.conf, so recovery is done at startup
- in my case the journal file was corrupt
- this means the journal is useless, and i deleted all files under /var/lib/mongodb/journal
That at least gives you the pre-crash db back.
indices
When creating an index at one point i got :
<pre>
mmap: can't map area of size 0 file: /var/lib/mongodb/_tmp/esort.1380464575.393277965//file.20
</pre>
C++
Programming mongodb in c/c++ means learning about binary json objects or BSON.
TODO : get samples from klopt.cpp in grid ...
BSON
Testing if a returned BSONObj is valid : It is not a pointer so NULL won;t work, use isValid() :
defrag is always
At startup it says :
This should be done with :
| changetitle | |
|---|---|
But this fails to work : probably because a reboot is needed
This works !!:
http://unix.stackexchange.com/questions/99154/disable-transparent-hugepages#99172
in short, put these into /etc/rc.local
| changetitle | |
|---|---|
Also after reboot, everything is ok !
numactl warning
When starting mongo cli, you might get a warning like this :
It only happens on servert, which obviously is "a NUMA machine". Numa has to do with allocation of memory and we should probably do what mongodb advises, but not system wide.
It turns out that /etc/init.d/mongodb has support for this but it is hijacked by systemd halfway and systemd does NOT support this. Systemd DOES suck !!
I still see no single advantage in systemd whatsoever !
So we have to do that ourselves. First make sure you installed numactl.
| changetitle | |
|---|---|
Now find the startup script for mongodb and edit the line with ExecStart:
| changetitle | |
|---|---|
The changes i made :
| changetitle | |
|---|---|
This does take care of the message.
Call to undefined method MongoDBDriverWriteConcern::isDefault
That is the top message running mongodb programs. It is because of a version mismatch between the low-level driver and the convenience driver.
The new way to use mongodb for php is to use the mongodb driver rather than the mongo driver :
On jessie the easiest way is still the 'legacy' way :
| changetitle | |
|---|---|
On stretch that package is not even available anymore so : However that package is nearly unusable so you need a second layer to be installed with composer.
stretch :
If you run this on a vanilla stretch install it will print :
| changetitle | |
|---|---|
And indeed the version listed above is not 1.3.0. However it is more effort to upgrade mongo-ext than to downgrade the other :
| changetitle | |
|---|---|
composer is a local install tool, you have to be in the project directory to use the installed library.