我希望能够移除数组元素[1]以进行服务并收费。问题是任何时候其他元素
包含相同的值,所有元素都被删除即。 [1] - [8]和[10] - [15]。我试图通过指定
来限制它 括号中的元素标识符,也不起作用。
如果没有重复值,则以下代码可以正常工作。我可以毫无问题地删除[1]和[9]。
$forward = $_POST['id'];
$conn = new Mongo('localhost');
$db = $conn->pianos;
$collection = $db->names;
$criteria = array('_id' => $forward,);
// Find single document by ID Variable
$obj = $collection->findOne($criteria);
// define variables for Array objects
$service = $obj['service'] [$i];
$charge = $obj['charge'] [$i];
$serv = array('service' => $service, 'charge' => $charge);
$upsert = true;
$collection->update($obj, array('$pull' => $serv) ,array("upsert" => $upsert));
PhpMoAdmin提供的文档结构如下所示。
[$_id] => MongoID Object (
[$id] => 4e3a8a457bfb8b2817000002
)
[fname] => Billy Bob
[lname] => Thornton
[service] => Array (
[0] => Tune 2 pianos
[1] => Tuned
[2] => Tuned
[3] => Tuned
[4] => Tuned
[5] => Tuned
[6] => Tuned
[7] => Tuned
[8] => Tuned
[9] => Called
[10] => Tuned
[11] => Tuned
[12] => Tuned
[13] => Tuned
[14] => Tuned
[15] => Tuned
)
[charged] => Array (
[0] => 100.00
[1] => 50.00
[2] => 50.00
[3] => 50.00
[4] => 50.00
[5] => 50.00
[6] => 50.00
[7] => 50.00
[8] => 50.00
[9] => 0.00
[10] => 50.00
[11] => 50.00
[12] => 50.00
[13] => 50.00
[14] => 50.00
[15] => 50.00
)
答案 0 :(得分:2)
MongoDB没有修改器操作。
您可以为所有条目添加服务日期,以便所有条目都变得唯一。此外,从设计的角度来看,最好将数据子文档存储在包含服务名称,数据和数据的子文档中。充电(以及之后添加的任何内容)。
使用当前的“架构”,您唯一能做的就是检索整个文档,在客户端修复它并将其写回。这不是原子的,所以请确保只有一个用户可以在特定时间修改文档。