MongoDB with PHP: nest されたキーバリューの削除

updated: 2018/10/23

 環境: Laravel 5.7.6 PHP7

 

f:id:monteecristoo:20181022200412p:plain

特定のコレクション内のcomments array の特定の _id をもつバリューを削除したい。

$db = (new MongoDB\Client)->YOUR_DB;

$collection = $db->YOUR_COLLECTION;

f:id:monteecristoo:20181023203117p:plain

$pid は削除したい pixieImage collection の id

$cid は削除したいcomment の id  下記例ではcommentsの一番目の_id

f:id:monteecristoo:20181023194406p:plain

MongoDB\BSON\ObjectId を自前で作るなら

use MongoDB;

f:id:monteecristoo:20181023202911p:plain

参考:

stackoverflow.com

MongoDB\Collection::updateOne() — PHP Library Manual 1.4

 

https://docs.mongodb.com/manual/reference/operator/update/pull/index.html

 

PHP: MongoDB\BSON\ObjectId - Manual

https://docs.mongodb.com/php-library/current/reference/method/MongoDBCollection-findOne/

Laravel-Mix: node_modules 内のpackageのcssをコンパイルする

webpack.mix.js

で指定したコンパイルするscss内でインポートさせる

例:

node_module perfect-scrollbar の cssコンパイルしたい

  @import '~perfect-scrollbar/css/perfect-scrollbar.css';

参考:

stackoverflow.com

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ⑩ 特定のcollectionのネストされたキーの値を取得する

mysql の select XX に当たるのが mongoDB では projection

f:id:monteecristoo:20201019100602p:plain



'user_id' => 1 は true という意味

参考:

github.com

jenssegers/laravel-mongodb を使わないで mongoDB php driver を使うなら

f:id:monteecristoo:20201019100820p:plain



f:id:monteecristoo:20181015183339p:plain

参考:

MongoDB\Collection::findOne() — PHP Library Manual 1.4

phpタブ内

Project Fields to Return from Query — MongoDB Manual

 

1段以上ネストされたキーから取得したい場合はドットでチェーンさせていく

f:id:monteecristoo:20201019101005p:plain



 

MongoDB PHP driver

新しい ObjectId を作る

@mongo shell

  ObjectId();

@php

  new MongoDB/BSON/ObjectId;

PHP側でnamespaceを使っていたら…

use MongoDB\BSON\ObjectID;

 

new ObjectID;

 

参考:

BSON — PHP Library Manual 1.4

PHP: MongoDB\BSON\ObjectId - Manual

vue-infinite-loading と laravel の連携

updated: 2018/10/3

@vue

f:id:monteecristoo:20181003045135p:plain

?page=1 のクエリを添えてGET

@laravel

f:id:monteecristoo:20181003045259p:plain

$request->page で欲しいpage番号を指定

forPage() はpage1 と page2以降で返り値の挙動に差があるエラーがあるので下記参照

github.com

gist.github.com

Laravel 5.x Factory と Faker で relational なモックアップを作る。

updated: 2021/6/1

 

jenssegers/mongodb の mysql relationships で使用可能なことが確認済み

 

php artisan make:factory FACTORY_NAME

factory1

f:id:monteecristoo:20201022131507p:plain



 factory2

f:id:monteecristoo:20181001204844p:plain

seeds/DatabaseSeeder.php

f:id:monteecristoo:20201022131857p:plain


おまけ

ランダムな文字列を生成

return [

  'randomText' => $faker->regexify('[A-Za-z0-9]{' . mt_rand(4, 20) . '}'

]

 



php artisan db:seed

これでfaker モックアップ + user と 某model のrelationship に整合性が取れたDBができる。

参考:

monteecristoo.hatenablog.com

PHPフレームワーク Laravel実践開発

PHPフレームワーク Laravel実践開発

 
 

 

stackoverflow.com

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7 ⑨ Mysql relationships で mongoDB と mysql 上のデータを合わせて返す。

updated: 2018/10/1

Mysql relationships については下記参照

monteecristoo.hatenablog.com

mongoDBとMysqlをjoin して返したい場合、mongoDBはjoinに対応していないので、

f:id:monteecristoo:20180930202813p:plain

PixieImage は mongoDB上のデータベース

user はmysql のデータベース

 さらにこれを pagination のように動作させたいなら collection は paginate が使えないので forPage() メソッドを使う。

参考:

laravel.com