如何在Mongo中按键删除文档中的元素?

时间:2017-05-20 20:00:59

标签: php mongodb mongoose

我有以下Mongo文件:

"parameters" : {
    "59209f26470e6c8d0b3c986b" : {
        "Speed" : "900"
    },
    "59209f31470e6cb90e3c986b" : {
        "Weight" : [
            "1"
        ]
    }
},

我需要按键parameters"59209f26470e6c8d0b3c986b"删除元素。怎么做?

我试过了:

$new = array('$pull' => array("parameters" => $this->mongoDbID($id)));

$this->collection->update(array("parameters" => []), $new);

我也试过了:

$this->collection->update(array(), array('$pull'=>array('‌​parameters'=> $id)));

我也试过了:

$this->collection->update(
            array(),
            array('$pull' => array('parameters' => $id, []))
        );

它给了我:

  

localhost:27017:无法将$ pull应用于非数组值

完整文档:

 > db.objects.find().pretty()
    {
        "_id" : ObjectId("5920b488470e6c910b3c986d"),
        "name" : "dadasd",
        "category_id" : "591efb0f470e6ccc143c9873",
        "prototype_id" : "591ed2f0470e6ccc143c986e",
        "parameters" : {
            "5920a907470e6cf80d3c986b" : {
                "adadad" : "dad"
            }
        },
        "parameters_type" : {
            "5920a907470e6cf80d3c986b" : NumberLong(1)
        },
        "available" : "1"
    }

2 个答案:

答案 0 :(得分:0)

根据您的答案解决方案:

        let trigger = UNLocationNotificationTrigger(region:region, repeats:true)
        let delete = UNNotificationAction(identifier: "DeleteAction",
                                                title: "Delete", options: [.destructive])
        let category = UNNotificationCategory(identifier: “testcategory”, actions: [delete], intentIdentifiers: [], options: [])
        center.setNotificationCategories([category])
        let content = UNMutableNotificationContent()
        content.title = “This is a test“
        content.body = "You're at your destination"
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = “testcategory”
        let identifier = "testnotif"
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
            center.add(request, withCompletionHandler: { (error) in
                if let error = error {
                    print(error)
                } else {
                    //successful
                }
            })
  

或者

$this->parametersCollection->update(array('_id'=>$id),array('$unset'=>array('parameters_Id' => $null)));

ex:$ key =" 59209f26470e6c8d0b3c986b"

php-mongodb的一些参考链接

1。http://php.net/manual/en/mongocollection.remove.php

2。http://php.net/manual/en/class.mongocollection.php

3。http://php.net/manual/en/mongocollection.update.php

4。http://php.net/manual/en/mongocollection.insert.php

5。https://docs.mongodb.com/manual/tutorial/update-documents/

答案 1 :(得分:0)

您要做的是$unset(文档here

现在示例:

$docId = $this->mongoDbID('5920b488470e6c910b3c986d');
$parameterId = '59209f26470e6c8d0b3c986b';
$this->collection->update(
  ['_id' => $docId],
  ['$unset' => [
      'parameters.'.$parameterId => null, 
      'parameters_type.'.$parameterId => null
  ]]
);

或:

$parameterId = '59209f26470e6c8d0b3c986b';
$this->collection->update(
  [],
  ['$unset' => [
      'parameters.'.$parameterId => null, 
      'parameters_type.'.$parameterId => null
  ]],
  ['multiple' => true]
);