Laravel-mongodb: Custom _id with a pre-generated _id

Created on 8 Jun 2014  路  11Comments  路  Source: jenssegers/laravel-mongodb

Hi!

I have an ObjectId generator in Javascript, which generates "_id" like "5394d7db560c9869a1000002", But when I pass this "_id" to "Moloquent" to do an insert it stores as a string instead of an ObjectId like "ObjectId('5394d7db560c9869a1000002')".

The only way I have found to solve this is to make a new MongoId with the pre-generated "_id".

$this->_id  = new MongoId($obj['_id']);
$this->name = $obj['name'];
$this->save();

There is another more elegant way to do this? Thanks!

PD: (Sorry for my english)

Most helpful comment

@pirmax

I have find solution for same. Try below syntax.

$id = new \MongoDB\BSON\ObjectID($id);

As per my code, it will look like below this

$user = new User;
$user->companyId = new \MongoDB\BSON\ObjectID($companyId);
$user->save();

All 11 comments

Should be fixed in the last (master) version.

Perfect! @jenssegers, thank you very much! Nice work!

I don't think this is a correct approach for this issue jenssegers.
MongoDB allows all kind of _id (String, Int, ...) and like WiredPrairie said in this topic, MongoId objects are only there for situations where you don't already have a unique key.
So, I think the MongoId object should be a default only in case of an empty _id, but not an obligation.

Personally, I use a lot of unique string keys.

@v-six What would you change?

@jenssegers I'll give you a pull request in a few days :-).

@jenssegers
I hope you are doing good!

I have an issue with relationship id, which is store as string.

How I can store that companyId as Object id?

$user = new User;
$user->companyId        = $companyId;
$user->save();

@jimeshgajera I want this too!

@pirmax

I have find solution for same. Try below syntax.

$id = new \MongoDB\BSON\ObjectID($id);

As per my code, it will look like below this

$user = new User;
$user->companyId = new \MongoDB\BSON\ObjectID($companyId);
$user->save();

Thanks @jimeshgajera !

Hi!

I have an ObjectId generator in Javascript, which generates "_id" like "5394d7db560c9869a1000002", But when I pass this "_id" to "Moloquent" to do an insert it stores as a string instead of an ObjectId like "ObjectId('5394d7db560c9869a1000002')".

The only way I have found to solve this is to make a new MongoId with the pre-generated "_id".

$this->_id  = new MongoId($obj['_id']);
$this->name = $obj['name'];
$this->save();

There is another more elegant way to do this? Thanks!

PD: (Sorry for my english)

@nanchet

you can add your id in your create object as id field and add this code in model

protected static function boot()
{
parent::boot();

    static::creating(function($order) {
        $order->_id = $order->id;
    });
}

@pirmax

I have find solution for same. Try below syntax.

$id = new \MongoDB\BSON\ObjectID($id);

As per my code, it will look like below this

$user = new User;
$user->companyId = new \MongoDB\BSON\ObjectID($companyId);
$user->save();

鉂わ笍鉂わ笍
Hug you! Thank you very much!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pirmax picture pirmax  路  3Comments

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

lgt picture lgt  路  3Comments

yupangestu picture yupangestu  路  3Comments

YSimple picture YSimple  路  3Comments