This is my first time shoving objects into Mongo - I am using PHP. I know that MongoDB adds the _id
variable to an array while inserting using the MongoCollection::insert()
function. the problem is this:
if I define a public variable named _id
the variable remains NULL
upon insert
class MognoTest {
public _id;
public foo;
public function __construct(){
$this->foo = 'bar';
}
}
$obj = new MongoTest();
$Mongo->collection->insert($obj);
var_dump($obj)
$found_obj = $Mongo->collection->findOne();
var_dump($found_obj);
The var_dump()
s on $obj
and $found_obj
both return an object with _id=NULL
. If I comment out _id in the class definition, the code runs properly and both var_dump()
s print a MongoID.
I want to define in the class an _id to make my code clearer and make my code hinting on Netbeans work properly. Is there something I am overlooking?
_id
before inserting it into the database? – maialitharMongoID
auto-generated by the MongoDB as my primary key and I'll be using in my code later on. – Rhett N. Lowe