Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

I get the question how to interact with MongoDB at least once a month. So here is the simple version.

You will need to have the Mongo Functions installed on your web server see the installation guide (http://php.net/manual/en/mongo.installation.php)

 

Connecting to the database:

Just like any other database you need to specify an IP, port, database, username, and password.

$connect_mongo = "mongodb://localhost:27017/mytestdatabasename";

$o_mongo_connection=  new Mongo($connect_mongo,array('username'=>'YourUsername','password'=>'YourPassword'));

 

You will need to assign that database to a var so you can reference it easily. 

$o_database = $o_mongo_connection->selectDB('yourdb');

 

Now thinking in the terms of normal database like MySQL or MS SQL you generally write a select statement against a table. In the case of MongoDB we are going to make a reference to the collection (sorta of like a table).

$o_collection = $o_database->collection; //grabs the default collection

$o_collection = $o_database->selectCollection('collection'); //grabs a specific collection.  Recommended

 

If you wanted to make a new collection you can do this:

$o_recipes = $o_database->recipes;

You now have a collection called recipes

To add a recipe:

$a_ingredients = array('Item' => '1 pound flour','Item' => '1 pound butter', 'Item' => '1 pound sugar');

$a_recipe = array(

        'title'     => 'Pound Cake',

        'description'   => 'They called it pound cake becuase it literally used a pound of butter, pound of sugar, and a pound of flour',

'ingredients'   => $a_ingredients,

        'create_date'  => new MongoDate() 

    );

 $o_recipes->insert($a_recipe);

 

Yes, you can simply pass it an array and that is your new document.

Please note that $recipe now has a new key called '_id' which was created for you and which you can now use to find the item in the database.

 

Reading and Finding

So to pull out the record you just made you could (wouldn't really need to do this since $recipes is already loaded into memory):

$id = $recipe['_id'];

$results = $o_recipes::findOne(array('_id' => $id));

 

To find all recipes that use 1 pound of flour:

$a_query = array('Item' => '1 pound flour');

// for a like use this: $a_query = array('Item' =>array('$regex' => '1 pound'));  //there are various ways to do a like some are  more optimal than others

 

$a_results = $o_recipes::find($a_query);

To iterate the array:

foreach ($a_results as $doc) {

    print_r($doc);

}

To Update

To update a recipe you pass back the recipe you found. the _id is the key here.

if your record has an _id then it is an update. if no _id then it is new.

if you pass in new array keys they will be added.

 

To Delete:

to remove a single document you will want to use the _id for the most part

$id = $recipe['_id']; //or you got it from a POST or GET

$o_recipes::remove(array('_id' => $id));

or

$o_recipes::remove(array('_id' => 777), array("justOne" => true));

 

References:

http://php.net/manual/en/mongo.core.php

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

I wrote a GeoIP service.

it will get the ip of the user hitting the page or you can attach ip= to the query string.

get vars to pass are:

no format  query modifiers will give you the following:

ip address matched to| loc id| country| state| city| zip | longitude | latitude

 

Formatting the Results:

format= xml,json,bar

 

Only the elements you want:

f=flags

Flags

  • i = ip 
  • d = locid
  • c = country
  • s = state
  • l = city (this is L for locality)
  • z = zip
  • n = longitude
  • a = latitude 

so if the query string had  &f=naz you would get back longitude, latitude , and the zip code

 

 

Some Examples:

This would get the zip, lng, lat for the IP 8.8.8.8 in json format:

path/index.php?format=json&f=naz&ip=8.8.8.8

 

This would get the zip ,lng, lat of the person hitting the web server  in json format:

path/index.php?format=json&f=naz

 

This would get the city, state of the person hitting the web server in bar format ( city|state):

path/index.php?format=bar&f=ls