MongoDB

Scalable document-oriented storage. JSON-style documents with dynamic schemas offer simplicity and power.

MongoDB supports various connectors of different quality for C, C#, C++, Erlang, Haskell, JavaScript, Java, Perl, PHP, Python, Ruby, and Scala. The company behind it, which was called 10gen until half a year ago, offers a commercial version with support. In the approximately five years since its inception, the C++-based database has likely become the most widespread NoSQL database, used by organizations such as MTV, CERN, and The New York Times.

Notes

Geo from Python

Very simple to execut near query with mongo for simple apps

Create an index

db.posts.createIndex({ location: "2dsphere" })

Then, ensure your data is stored in GeoJSON format, like this:

db.posts.insertOne({
    title: "A post near NYC",
    location: { type: "Point", coordinates: [-73.9000, 40.7000] }
})

Once the index is created, your original geoNear query should work:

db.runCommand({
    geoNear: "posts",                   // Specifies the collection to search in
    near: [-73.9000, 40.7000],           // The point (longitude, latitude) to search near
    spherical: true,                     // Indicates the coordinates are in a spherical system (WGS84)
    maxDistance: 10000,           // Maximum distance in meters
    distanceMultiplier: 6378137          // Converts results to meters (Earth’s radius in meters)
});

Equivalent Python Code (pymongo)

from pymongo import MongoClient
from bson.son import SON

# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["your_database"]

# Ensure a geospatial index (for GeoJSON, use '2dsphere')
db.places.create_index([("loc", "2dsphere")])

# Run the geoNear command
query = SON([
    ("geoNear", "places"),
    ("near", {"type": "Point", "coordinates": [37.617000, 55.755000]}),  # GeoJSON format
    ("spherical", True),
    ("maxDistance", 0.1)  # Distance in meters
])

result = db.command(query)

# Print results
for place in result["results"]:
    print(place)

Admin

mongocli drop db

use db
db.dropDatabase()

Mongo restore

mongorestore --drop --host 192.168.100.33 --port 27017 -u "user" -p "password" --authenticationDatabase "authdb" -d HOST FOLDER

Users

use mydbname;
db.getUsers()

db.createUser(
    {
        user: "imauser",
        pwd: "mypass",
        roles: []
    }
)

Grant role

db.grantRolesToUser("imauser", ["myrole"])

Backup mongo example

$ mongodump --port 27017 -u "myuser" -p "mypass" --authenticationDatabase "authdb" --db mydb --out /tmp/mo

Connect to Mongo with mongo-shell

$ mongo --port 27017 -u "myuser" -p "mypass" --authenticationDatabase "mydbname" somename
> show dbs

Mongo Docker Setup

Setup a replica set with Docker containers

# docker run -p 30001:30001 --name mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set --port 30001
# docker run -p 30002:30002 --name mongo2 --net my-mongo-cluster mongo mongod --replSet my-mongo-set --port 30002
# docker run -p 30003:30003 --name mongo3 --net my-mongo-cluster mongo mongod --replSet my-mongo-set --port 30003

Start containers

docker start mongo1[2,3]
Important:
  • The port inside the container must be the same as the one mapped externally; otherwise, the driver may have issues connecting to the replica set.

  • The client machine should be able to resolve mongo1, mongo2, etc., via DNS.

Cluster setup complete

Connect to a node for configuration

# docker exec -it mongo1 mongo --port 30001

Mongo shell

db = (new Mongo('localhost:30001')).getDB('test')
test
config = {"_id" : "my-mongo-set","members" : [{"_id" : 0,"host" : "mongo1:30001"},{"_id" : 1,"host" : "mongo2:30002"},{"_id" : 2,"host" : "mongo3:30003"}]}
rs.initiate(config)

Document is saved in the Mongo folder

Connect to Replica Set

mongo 'mongodb://$(basename $BASH_SOURCE):$(basename $BASH_SOURCE)@somename_mongo1_1:27001,somename_mongo2_1:27002,somename_mongo3_1:27003/somename?replicaSet=somename'  --authenticationDatabase 'somename'

Note: Different formats exist in different MongoDB versions. The following is for MongoDB 3.4.

my-mongo-set:SECONDARY> rs.slaveOk()  # Allows reading from the secondary replica
my-mongo-set:SECONDARY> show dbs

See also the Python implementation in the moqscripts project: [MongoTest](http://vertx.io/docs/vertx-mongo-client/java/).

Activate authentication in Mongo Replica Setup

For a replica set, it is important to provide a key for authentication between instances.

mongod --keyFile <path-to-keyfile> --replSet <replicaSetName> --bind_ip localhost,<ip address of the mongod host>

Copy the key file or certificate to the machine and start MongoDB as shown above.

References: - [Update an Existing MongoDB Replica Set to Use Key Authentication](http://pe-kay.blogspot.de/2016/02/update-existing-mongodb-replica-set-to.html) - [MongoDB Internal Authentication Documentation](https://docs.mongodb.com/manual/core/security-internal-authentication/)

Example setup:

mkdir -p /etc/moq/mongo/
echo "qwerty" > /etc/moq/mongo/key
chmod 600 /etc/moq/mongo/key
chown 999:999  /etc/moq/mongo/key

Use the key file in the MongoDB startup command:

--keyFile /etc/moq/mongo/key

Docker restore database

Copy an example to the container

docker cp /somepath mongo1:/tmp/

Restore database

docker exec -it mongo1 mongorestore --port 30001 --drop -d /somepath /tmp/somepath/