Correct command for mongo console
db.project.updateMany(
{
"offers": {$ne: []},
"offers.productsWithInfo": {$exists: false},
},
[
{
$set: {
"offers": {
$map: {
input: "$offers",
in: {
$mergeObjects: [
"$$this",
{
productsWithInfo: {
$map: {
input: "$$this.products",
as: "product",
in: {
product: "$$product",
productInfo: null,
},
},
},
},
],
},
},
},
},
},
]
)
I transferred it into migration:
public function up()
{
$this->update(
self::COLLECTION,
[
'offers' => ['$ne' => []],
'offers.productsWithInfo' => ['$exists' => false],
],
[
'$set' => [
'offers' => [
'$map' => [
'input' => '$offers',
'in' => [
'$mergeObjects' => [
'$$this',
[
'productsWithInfo' => [
'$map' => [
'input' => '$$this.products',
'as' => 'product',
'in' => [
'product' => '$$product',
'productInfo' => null,
],
],
],
],
],
],
],
],
],
],
);
}
but after executing I have next data in “offers” property, instead of real data:
"offers": {
"$map": {
"input": "$offers",
"in": {
"$mergeObjects": ["$$this",
{
"productsWithInfo": {
"$map": {
"input": "$$this.products",
"as": "product",
"in": {
"product": "$$product",
"productInfo": null
}
}
}
}
]
}
}
},
How can I add the complex command into migration?
Or can I use raw command (like with MySQL and createCommand() + execute()) ?