第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

PHP MongoDB\Client updateOne 向現(xiàn)有文檔數(shù)組添加新字段

PHP MongoDB\Client updateOne 向現(xiàn)有文檔數(shù)組添加新字段

PHP
幕布斯7119047 2021-09-18 19:36:36
我正在努力更新文檔并將新元素(字段)添加到現(xiàn)有數(shù)組中,而不會在更新時(shí)丟失數(shù)組元素。這是我的代碼,如果它不存在(upsert),它會將一個(gè)新文檔插入到集合中:$updateResult = $collection->findOneAndUpdate(            [                'recording-id' => $out['recording']->id            ],            ['$set'  => [                'release'            =>    [                    'id'            => $out['release']->id,                     'title'         => $out['release']->title,                    'date'          => $out['release']->date,                    'country'       => $out['release']->country                ],                'artist'            => [                    'id'            => $out['artist']->id,                    'name'          => $out['artist']->name,                ],                'recording'         => [                    'id'            => $out['recording']->id,                    'title'         => $out['recording']->title,                    'score'         => $out['recording']->score,                    'length'        => $out['recording']->length,                    'release-count' => count($out['recording']->releases),                ],我注意到有“$addToSet”和“$push”命令,并且可以使用幫助來了解這兩個(gè)命令之間的區(qū)別。如果要更新的文檔中不存在該字段,則 $push 添加具有該值的數(shù)組字段作為其元素。$addToSet 運(yùn)算符將一個(gè)值添加到數(shù)組中,除非該值已經(jīng)存在,在這種情況下 $addToSet 對該數(shù)組沒有任何作用。我做了一些谷歌搜索,并閱讀了 MongoDB/Client UpdateOne 函數(shù),但似乎無法找到將這些字段附加到現(xiàn)有數(shù)組的方法。我得到的錯(cuò)誤是:Fatal error:  Uncaught MongoDB\Driver\Exception\BulkWriteException: The field 'artist' must be an array but is of type object in document {_id: ObjectId('5d1a6aaf5ecc8001ee858f6c')} in ...我知道以下幾點(diǎn):它可能是我的文檔,因?yàn)樗皇侵旅e(cuò)誤所抱怨的正確數(shù)組。這可能是我的“findOneAndUpdate”格式,而我沒有正確地這樣做??赡軆烧呒娑兄覐囊婚_始就錯(cuò)了。任何見解或建設(shè)性的批評表示贊賞,只是不要火焰,請。
查看完整描述

1 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是我最終用來讓它做我想做的工作的代碼。


這是一個(gè)簡單的問題,只需設(shè)置您的字段名稱和值,mongo 將更新找到的記錄,并將其替換為發(fā)送給它的數(shù)組。無需推動或任何東西。


function firstFindMongoUpdate($out) {

$collection         = (new MongoDB\Client)->stream->musicbrainz;

$updateResult = $collection->findOneAndUpdate(

        [

            'recording-id'          => $out['recording']->id

        ],

        ['$set'  => [


                'query'             => $out['query'],


                'release'           =>    [

                    'id'            => $out['release']->id, 

                    'title'         => $out['release']->title,

                    'date'          => $out['release']->date,

                    'country'       => $out['release']->country,

                    'label'         => $out['release']->label,

                ],

                'artist'            => [

                    'id'            => $out['artist']->id,

                    'name'          => $out['artist']->name, 

                    'wiki'          => $out['artist']->wiki,

                ],

                'recording'         => [

                    'id'            => $out['recording']->id,

                    'title'         => $out['recording']->title, // sometimes contains apostophe ie; Bill‘s Love (option ] key)

                    'score'         => $out['recording']->score,

                    'length'        => $out['recording']->length,

                    'release-count' => count($out['recording']->releases)

                ],

                'release-group'     => [

                    'id'            => $out['release-group']['id'],

                    'title'         => $out['release-group']['title'],

                    'first-release-date'=>$out['release-group']['first-release-date'],

                    'primary-type'  => $out['release-group']['primary-type'],

                    'musicbrainz'   => $out['release-group']['musicbrainz'],

                    'url-rels'      => $out['release-group']['url-rels'],

                    'coverart'      => $out['release-group']['coverart'],

                    'wiki'          => $out['release-group']['wiki']

                ],

                'execution'         => [ 

                    'artistQuery'   => $out['execution']->artistQuery,

                    'recordingQuery'=> $out['execution']->recordingQuery,

                    'time'          => $out['execution']->time

                ]


        ]

    ],

    ['upsert'           => true,

    'projection'        => 

        [ 

        '_id'             => 1,

         'query'          => 1,

         'release'        => 1, 

         'artist'         => 1,

         'recording'      => 1,

         'release-group'  => 1,

         'execution'      => 1


        ],

        'returnDocument'    => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,

    ]

);



查看完整回答
反對 回復(fù) 2021-09-18
  • 1 回答
  • 0 關(guān)注
  • 360 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號