"_id"및 "ProductLot"이 있습니다. ProductLot (있는 경우) 또는 존재하지 않는 경우 새 Lot 요소를 추가하면 Lot 배열에서 수량을 "0"에서 "1"로 업데이트 할 수 있습니까?MongoDB : 배열에 삽입 및 업데이트
"_id" : ObjectId("5462e44c599e5c6c1300000a"),
"LocationName" : "Putaway",
"Owner" : "",
"Status" : "1",
"Scrap" : "0",
"Lot" : [{
"Qty" :"6",
"ProductLot" : ObjectId("5462dbd9599e5c200e000000"),
"Product" : ObjectId("543ca7be4cf59d400c000004"),
"Movement" :[ {"OriginId" : "266",Qty:2,Type:"DN"},
{"OriginId" : "267" , Qty:1 , Type:"DN"},
{"OriginId" : "2" , Qty:3 , Type:"IM"},
]
},
{
"Qty" :"0",
"ProductLot" : ObjectId("5462dbd9599e5c200e000003"),
"Product" : ObjectId("543ca7be4cf59d400c000004"),
"Movement" :[ {"OriginId" : "266",Qty:2,Type:"DN"},
{"OriginId" : "267" , Qty:1 , Type:"DN"},
{"OriginId" : "2" , Qty:-3 , Type:"IM"},
]
}]
}
EG : I "는 ProductLot"가지고 그 수량 0 1 업데이트해야하므로 본 ObjectId가 배열 ("5462dbd9599e5c200e000000을"); 그러나 "ProductLot": ObjectId ("5462dbd9599e5c200e00000a")를 배열에서 사용할 수 없으므로 배열에 새 요소를 추가해야합니다. 모든 시간에 추가 배열을 생성 업데이트되지
PHP 코드 :
이 $inventoryId = new \MongoId($_POST["inventoryid"]);
$productLotId = new \MongoId($invlotid);
$originId = $_POST['id'];
//$lotcontent = [ /* whatever this looks like */ ];
$lotcontent = array(
'Qty' => $invqty,
'ProductLot' => new \MongoId($invlotid),
'Product'=>$invproduct,
'Movement'=> array(
array(
'OriginId' => $_POST['id'],
'Qty' => $invqty,
'Type'=> 'DN',
), )
);
$invcolname = 'Inventory';
$result = $mongo->$dbname->$invcolname->update(
// Match an inventory without the specific ProductLot/OriginId element
array(
'_id' => $inventoryId,
'Lot' => array(
'$not' => array(
'$elemMatch' => array(
'ProductLot' => $productLotId,
//'OriginId' => $originId,
),
),
),
),
// Append a new element to the Lot array field
array('$push' => array('Lot' => $lotcontent))
);
$movementcontent = array(
'OriginId' => $_POST['id'],
'Qty' => $invqty,
'Type'=> 'DN',
);
$result = $mongo->$dbname->$invcolname->update(
// Match an inventory without the specific ProductLot/OriginId element
array(
'_id' => $inventoryId,
'Lot' => array(
//'$not' => array(
'$elemMatch' => array(
'ProductLot' => $productLotId,
'Movement'=>array(
'$not' => array(
'$elemMatch' => array(
'OriginId' => $originId,
)
)
)
),
//),
),
),
// Append a new element to the Lot.Movement array field
array('$push' => array('Lot.$.Movement' => $movementcontent))
);
$result = $mongo->$dbname->$invcolname->update(
// Match an inventory with a specific ProductLot/OriginId element
array(
'_id' => $inventoryId,
'Lot' => array(
'$elemMatch' => array(
'ProductLot' => $productLotId,
//'OriginId' => $originId,
'Movement'=>array(
'$elemMatch' => array(
'OriginId' => $originId,
)
)
),
),
),
// Update the "Qty" field of the first array element matched (if any)
//array('$set' => array('Lot.$.Qty' => 'Updated')),
array('$set' => array('Lot.$.Movement.$.Qty' => $invqty)),
array('upsert' => true));
사람이 제발 도와주세요은이 문제를 해결하려면? 다음 lot
요소가 다른 간주 될 수 있기 때문에 $addToSet
를 사용
$ 세트는 당신이 찾고있는 것입니다! Google은 친구입니다 – Sekai