0

Am working on a project using codeigniter and mongodb.How can i fetch data from mongodb based on _id.I mean how can i give the where condition in '$query=$this->mongo_db->get_where('photos', $where)'.I need to fetch the photos_path based on the 'id'.Please help

Here is the array format of my mongodb collection:-

    Array
(
    [0] => Array
    (
        [_id] => MongoId Object
            (
                [$id] => 510f7e0d2217f4fc03000000
            )

        [key] => organization_img
        [photos_path] => uploads/img_51359969805.gif
    )

[1] => Array
    (
        [_id] => MongoId Object
            (
                [$id] => 5111ef2f2217f4280a000000
            )

        [key] => organization_img
        [photos_path] => uploads/image.png
    )

)

Thanks

VeNaToR
  • 97
  • 1
  • 3
  • 10

3 Answers3

2

This query is perfect for getting a single record from collection with codeigniter using condition

$this->mongo_db->where('_id', $mongoid)->find_one('attributes')
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
1

I don't know how CodeIgniter's MongoDB API is different than the one from the driver, but you have to instantiate a new instance of MongoId and pass it to the query, like this:

$m = new MongoClient(...);
$col = $m->selectCollection(...);

$cursor = $col->findOne(array('_id' => new \MongoId($id)));

This is due to how MongoDB saves its _id column. http://docs.mongodb.org/manual/core/object-id/

If you were to supply a simple string as the _id column when creating a new document, you wouldn't have to instantiate a new object of MongoId every time you query for it. Keep in mind that you have to care about unique identities and cardinalities on your own if you do that, since MongoDB does not provide the option of AUTO_INCREMENT for certain colums like relational tables (MySql, PgSql, ...) do. Instead it gives each document a uniquely generated id object to act as the primary key.

The reason for the _id column to use generated objects not related to the collection itself as default is, uhm, because internally it is a bunch of stitched bytes, and comparing raw bytes for sorting for example is way faster than interpreting it as string characters while comparing, having to mess around with alphabetical orders and the like, while raw bytes can be compared logically, which is way faster. And believe me, you WILL want it to be faster when you query 100,000,000+ documents, which is usual for decent sized MongoDB instances. Using a technique like AUTO_INCREMENTed primary keys also won't allow for sharded databases easily, since you want to make sure that each index follows each other when inserting a new document, which will sure block other insertions within the same collection. This is why generated objects instead of AUTO_INCREMENT keys are used.

Anh Nhan Nguyen
  • 318
  • 1
  • 8
0

You can simply use:

$query=$this->mongo_db->get_where('photos', 
    array('_id' => new \MongoId($_id_of_another_row))
)

If the $_id_of_another_row is already a MongoId (if($_id_of_another_row instanceof \MongoId)) then you don't need to wrap it again and can use it straight away.

Wrapping it in the MongoId will create the following representative structure in MongoDB itself for the query document (the code above basically):

[_id] => MongoId Object(
    [$id] => 5111ef2f2217f4280a000000
)

I must admit I have never used CI but their API seems to just extend MongoDBs own PHP driver.

As reference here is a good question on here detailing example code snippets and tutorial links to go through: MongoDB and CodeIgniter

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146