MongoDB integration #1438

Merged
merged 56 commits into from Dec 5, 2013

Conversation

Projects
None yet
7 participants
@klimov-paul
Member

klimov-paul commented Dec 5, 2013

Relates to #11

Here is a MongoDB integration.

Note: I can not name myself an expert in MongoDB, so it could be I have forgotten or misplaced something, it would be nice if someone experienced with MongoDB review this PR.

Suggestions and critics are welcome.

klimov-paul added some commits Nov 25, 2013

Mongo Collection "findAll" method removed.
Logging and profiling for Mongo Query added.
@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 6, 2013

Agreed MongoDB is the convention

Sammaye commented Dec 6, 2013

Agreed MongoDB is the convention

@klimov-paul

This comment has been minimized.

Show comment
Hide comment
@klimov-paul

klimov-paul Dec 6, 2013

Member

OK, I wll perform renaming.

Member

klimov-paul commented Dec 6, 2013

OK, I wll perform renaming.

@klimov-paul

This comment has been minimized.

Show comment
Hide comment
@klimov-paul

klimov-paul Dec 6, 2013

Member

Done

Member

klimov-paul commented Dec 6, 2013

Done

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Dec 6, 2013

Member

yii2-mongodb repo and package created.

Member

qiangxue commented Dec 6, 2013

yii2-mongodb repo and package created.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 18, 2013

Authentication failed on database '' with username '': auth fails

Every time I get this error, but when I am using my own class for mongo db It's okey with the same config.

asofter commented Dec 18, 2013

Authentication failed on database '' with username '': auth fails

Every time I get this error, but when I am using my own class for mongo db It's okey with the same config.

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 18, 2013

What does your code/config look like?

I would imagine your personal class is quite different. It sounds like you are not putting the credentials in the right fields.

Sammaye commented Dec 18, 2013

What does your code/config look like?

I would imagine your personal class is quite different. It sounds like you are not putting the credentials in the right fields.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 18, 2013

'mongo' => [
            'class' => 'common\components\MongoDb',
            'dsn' => 'mongodb://admin:SOME_PASSWORD@localhost:27017/pgate',
        ],
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://admin:SOME_PASSWORD@localhost:27017/pgate',
        ],

and init of component

 if(class_exists("Mongo")) {
            $this->conn = new \Mongo($this->dsn);
            $database = $this->database;
            $this->db = $this->conn->$database;
            $this->db->setProfilingLevel(\MongoDB::PROFILING_SLOW);
        } else {
            throw new Exception("You don't have Mongo drivers.");
        }

asofter commented Dec 18, 2013

'mongo' => [
            'class' => 'common\components\MongoDb',
            'dsn' => 'mongodb://admin:SOME_PASSWORD@localhost:27017/pgate',
        ],
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://admin:SOME_PASSWORD@localhost:27017/pgate',
        ],

and init of component

 if(class_exists("Mongo")) {
            $this->conn = new \Mongo($this->dsn);
            $database = $this->database;
            $this->db = $this->conn->$database;
            $this->db->setProfilingLevel(\MongoDB::PROFILING_SLOW);
        } else {
            throw new Exception("You don't have Mongo drivers.");
        }
@klimov-paul

This comment has been minimized.

Show comment
Hide comment
@klimov-paul

klimov-paul Dec 19, 2013

Member

What version of PHP Mongo extension do you have?

Member

klimov-paul commented Dec 19, 2013

What version of PHP Mongo extension do you have?

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

mongo
MongoDB Support enabled
Version 1.4.4
SSL Support enabled
Streams Support enabled

asofter commented Dec 19, 2013

mongo
MongoDB Support enabled
Version 1.4.4
SSL Support enabled
Streams Support enabled

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

The version shouldn't matter here since both are using the dsn.

Hmmm, need to test this

Sammaye commented Dec 19, 2013

The version shouldn't matter here since both are using the dsn.

Hmmm, need to test this

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

It's some problem with script, because my script is working and I can connect to the database easily.

asofter commented Dec 19, 2013

It's some problem with script, because my script is working and I can connect to the database easily.

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Well the only real difference is that the yii ext uses MOngoClient and you use Mongo which is deprecated.

Can you try using MOngoClient a sec?

Sammaye commented Dec 19, 2013

Well the only real difference is that the yii ext uses MOngoClient and you use Mongo which is deprecated.

Can you try using MOngoClient a sec?

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Also can you look in the log and tell us what the dsn is there, it should log the dsn just before opening the connection: https://github.com/yiisoft/yii2/blob/master/extensions/yii/mongodb/Connection.php#L226

Sammaye commented Dec 19, 2013

Also can you look in the log and tell us what the dsn is there, it should log the dsn just before opening the connection: https://github.com/yiisoft/yii2/blob/master/extensions/yii/mongodb/Connection.php#L226

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

and also I've inserted echo $this->dsn to make sure if dsn is right and it is good.

asofter commented Dec 19, 2013

and also I've inserted echo $this->dsn to make sure if dsn is right and it is good.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

found, problem was because of adding database name to dsn

        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://admin:PASSWORD@localhost:27017',
            'defaultDatabaseName' => 'pgate'
        ],

asofter commented Dec 19, 2013

found, problem was because of adding database name to dsn

        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://admin:PASSWORD@localhost:27017',
            'defaultDatabaseName' => 'pgate'
        ],
@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

Also, Will you add functionality for auto incremental ids?

asofter commented Dec 19, 2013

Also, Will you add functionality for auto incremental ids?

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

You mean: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ ?

It goes against the fundamentals of MongoDB and lowers performance drastically and builds lock on distributed envos, hence why MongoDB does not do AI IDs so it is up to @klimov-paul if he wants to add it, personally I have no intention of adding such a thing to Yii2.

MongoDB should be used without AI IDs

Sammaye commented Dec 19, 2013

You mean: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/ ?

It goes against the fundamentals of MongoDB and lowers performance drastically and builds lock on distributed envos, hence why MongoDB does not do AI IDs so it is up to @klimov-paul if he wants to add it, personally I have no intention of adding such a thing to Yii2.

MongoDB should be used without AI IDs

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

That it didn't work in the dsn like that might mean there might be a problem with the regexing of the database name from the dsn cos it should be fine to add the db name to the dsn: http://www.php.net/manual/en/mongoclient.construct.php so it might be that it isn't picking out the right database from the regex

Sammaye commented Dec 19, 2013

That it didn't work in the dsn like that might mean there might be a problem with the regexing of the database name from the dsn cos it should be fine to add the db name to the dsn: http://www.php.net/manual/en/mongoclient.construct.php so it might be that it isn't picking out the right database from the regex

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

Because when I want to show smth by id (page.php?id=ID)
I need to change _id to string

echo Html::a('<i class="glyphicon glyphicon-remove-circle"></i>', ['/domains/delete', 'id' => (string) $domain['id']]);

asofter commented Dec 19, 2013

Because when I want to show smth by id (page.php?id=ID)
I need to change _id to string

echo Html::a('<i class="glyphicon glyphicon-remove-circle"></i>', ['/domains/delete', 'id' => (string) $domain['id']]);
@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Yeah this is a known little thing, personally I work around it, besides you shouldn't need to do explicit casting, the url manager should able to cast it naturally, should.

One thing you can do is maybe make your own custom smaller id, but not an AI ID, something random like the ObjectID but not so hard to make.

Sammaye commented Dec 19, 2013

Yeah this is a known little thing, personally I work around it, besides you shouldn't need to do explicit casting, the url manager should able to cast it naturally, should.

One thing you can do is maybe make your own custom smaller id, but not an AI ID, something random like the ObjectID but not so hard to make.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Dec 19, 2013

Member

For a similar issue with elasticsearch I use redis to generate my IDs: http://redis.io/commands/incr
I use redis as cache anyway so I have that option. This is only in cases where users see the id.

Member

cebe commented Dec 19, 2013

For a similar issue with elasticsearch I use redis to generate my IDs: http://redis.io/commands/incr
I use redis as cache anyway so I have that option. This is only in cases where users see the id.

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

With elastic search I use the Hex of ObjectIDs works quite well

Sammaye commented Dec 19, 2013

With elastic search I use the Hex of ObjectIDs works quite well

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

But yeah redis is one that is used here

Sammaye commented Dec 19, 2013

But yeah redis is one that is used here

@asofter

This comment has been minimized.

Show comment
Hide comment
@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

That's not what it sounds like, thats the inc of the ObjectId and is definitely not unique and infact will rarely inc unless you are throwing many ops per second.

Sammaye commented Dec 19, 2013

That's not what it sounds like, thats the inc of the ObjectId and is definitely not unique and infact will rarely inc unless you are throwing many ops per second.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

This needs to be a good way of solving this problem which will be used by all users of Yii

asofter commented Dec 19, 2013

This needs to be a good way of solving this problem which will be used by all users of Yii

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Distributed envos are really not designed for AI IDs, trust me it is really really realllllllly hard to ensure one

Sammaye commented Dec 19, 2013

Distributed envos are really not designed for AI IDs, trust me it is really really realllllllly hard to ensure one

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

I know, but (string) prefix is not right way too.

asofter commented Dec 19, 2013

I know, but (string) prefix is not right way too.

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

It is actually, the ObjectId is perfect for distributed envos, not so good for sharding but it is still awesome

Sammaye commented Dec 19, 2013

It is actually, the ObjectId is perfect for distributed envos, not so good for sharding but it is still awesome

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

And what about relations $this->hasOne(Modules::className(), ['_id' => 'module_id']); ?

asofter commented Dec 19, 2013

And what about relations $this->hasOne(Modules::className(), ['_id' => 'module_id']); ?

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Relations should be fully catered to MongoId (ObjectId), last I looked at this extension.

Sammaye commented Dec 19, 2013

Relations should be fully catered to MongoId (ObjectId), last I looked at this extension.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

but they are not working

asofter commented Dec 19, 2013

but they are not working

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Hmm I will need to have a look at that, if not then that should be considered a bug since ObjectId is default primary key for MongoDB.

I will try and test as soon as I have a free moment.

Sammaye commented Dec 19, 2013

Hmm I will need to have a look at that, if not then that should be considered a bug since ObjectId is default primary key for MongoDB.

I will try and test as soon as I have a free moment.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Dec 19, 2013

Oh, It was my fault, they are working.

asofter commented Dec 19, 2013

Oh, It was my fault, they are working.

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Dec 19, 2013

Ok kool :)

Sammaye commented Dec 19, 2013

Ok kool :)

@klimov-paul klimov-paul deleted the klimov-paul:mongo branch Dec 26, 2013

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Jan 11, 2014

Does MongoDB AR support caching?

Yii::$app->getDb()->beginCache();
$model = Post::find($id)->one();
Yii::$app->getDb()->endCache();

And when It will be transaction support?

asofter commented Jan 11, 2014

Does MongoDB AR support caching?

Yii::$app->getDb()->beginCache();
$model = Post::find($id)->one();
Yii::$app->getDb()->endCache();

And when It will be transaction support?

@klimov-paul

This comment has been minimized.

Show comment
Hide comment
@klimov-paul

klimov-paul Jan 11, 2014

Member

Does MongoDB AR support caching?

No.

And when It will be transaction support?

MongoDb server itself does not provide transaction support, so this extension simply have no change to introduce it.

Member

klimov-paul commented Jan 11, 2014

Does MongoDB AR support caching?

No.

And when It will be transaction support?

MongoDb server itself does not provide transaction support, so this extension simply have no change to introduce it.

@asofter

This comment has been minimized.

Show comment
Hide comment
@asofter

asofter Jan 11, 2014

Will it have support caching?

Oh, okey, thanks

asofter commented Jan 11, 2014

Will it have support caching?

Oh, okey, thanks

@Sammaye

This comment has been minimized.

Show comment
Hide comment
@Sammaye

Sammaye Jan 11, 2014

Though it is possible to implement client powered transactions which may be a further extension for someone to make.

Sammaye commented Jan 11, 2014

Though it is possible to implement client powered transactions which may be a further extension for someone to make.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment