yii2-mongodb icon indicating copy to clipboard operation
yii2-mongodb copied to clipboard

How to convert from activequery to json query

Open donny26utama opened this issue 1 year ago • 4 comments

What steps will reproduce the problem?

$book = Book::find()->where(['=', 'status'. 'publish')->rawQuery();

What's expected?

db.getCollection("book").find({
    "status": "publish"
});

What do you get instead?

Additional info

Q A
Yii version 2.0.14
Yii MongoDB version 2.1
MongoDB server version 5.0.6-5
PHP version 7.4
Operating system windows

donny26utama avatar Jun 12 '23 10:06 donny26utama

Hi

$book = Book::find()->where(['status' => 'publish'])->one();

Please see https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail

ziaratban avatar Jun 12 '23 11:06 ziaratban

Hi

$book = Book::find()->where(['status' => 'publish'])->one();

Please see https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail

I think you misunderstand my question, my question is how to get a raw query from ActiveQuery, if you use a relational database like MySQL when $book = Book::find()->where(['status' => 'publish'])->getRawSql(); you will get SELECT * FROM books WHERE status = 'publish'. So, when I use NoSQL like MongoDB, my expectation is something like db.getCollection("book").find({ "status": "publish" }); or only JSON {"status": "publish"}

donny26utama avatar Jun 12 '23 12:06 donny26utama

I will be appreciate you to add this feature Sometimes it is great to check native query in db client and make sure that your query is correct Thanks :+1:

ivanflashacademy avatar Mar 17 '24 12:03 ivanflashacademy

Another alternative is to use MongoDB Debug Panel which will show us additional information for each command / query.

In OP's case, it will show the query like so:

    log.command({"count":"book","query":{"$and":[{"$and":[{"status",.....

prabowomurti avatar Jul 03 '24 05:07 prabowomurti