Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shibbir's avatar

Laravel / Mongodb - How to delete a single / multiple keys from the documents

In the mongodb database I have multiple documetns and each document contain this type of data:

{
  "_id": {
    "$oid": "636b7f0f889929ce62194c28"
  },
  "id": "SK7732.1825+1",
  "aff_link": "https://go.linkwi.se/z/11593-0/CD24352/?lnkurl=https%3A%2F%2Fwww.celestino.gr%2Fel-GR%2Fpanteloni-me-pietes-lefko%2Fprd%2FSK7732.1825%2F7%3Faffiliate%3Dlinkwise%26utm_source%3Dlinkwise%26utm_medium%3Dlw%26utm_campaign%3Dlw",
  "availability": "Y",
  "brand": "Celestino",
  "color": [],
  "date_add": 1667989263,
  "date_upd": 1674697681,
  "description": "Παντελόνι με πιέτες, κλείσιμο με φερμουάρ και κουμπί, θηλιές στη μέση, με τσέπες, λευκο",
  "image_link": "https://2ccdn.celestino.gr/small/[email protected]",
  "link": "https://www.celestino.gr/el-GR/panteloni-me-pietes-lefko/prd/SK7732.1825/7?affiliate=linkwise&utm_source=linkwise&utm_medium=lw&utm_campaign=lw",
  "part_number": "SK7732.1825.1",
  "price": "13,99",
  "product_type": "Παντελόνια/Παντελόνια",
  "sizes_in_stock_feed": "S",
  "title": "Παντελόνι με πιέτες SK7732.1825+1",
  "update_history": [
    {
      "id_feed": 169,
      "feed_name": "from linkwise",
      "date_upd": 1674689686,
      "field_history": []
    },
    {
      "id_feed": 170,
      "feed_name": "scrape info",
      "date_upd": 1674697681,
      "field_history": [
        {
          "field_name": "all_sizes",
          "removable": 1,
          "updatable": 1,
          "date_upd": 1674697681
        },
        {
          "field_name": "sizes_in_stock",
          "removable": 1,
          "updatable": 1,
          "date_upd": 1674697681
        },
        {
          "field_name": "breadcrumbs",
          "removable": 1,
          "updatable": 1,
          "date_upd": 1674697681
        },
        {
          "field_name": "breadcrumbs_url",
          "removable": 1,
          "updatable": 1,
          "date_upd": 1674697681
        },
        {
          "field_name": "color",
          "removable": 1,
          "updatable": 1,
          "date_upd": 1674697681
        }
      ]
    }
  ],
}

From this data I can get the data based on id .for example:

$get_product = DB::connection('mongodb')->collection( $collection )->where( 'id', $id )->first();

Now, Is there anyway I can delete single or multiple keys from the document based on id?

For example, I want to delete the aff_link and availability key.

0 likes
3 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@shibbir Does it work for you?

DB::connection('mongodb')->collection( $collection )->where( 'id', $id )->update( [ '$unset' => [ 'aff_link' => 1, 'availability' => 1 ] ] );
Shibbir's avatar

@tisuchi Yes, it seems working. Can you tell me why I use '$unset' here. Is there any documentation.

Thanks.

tisuchi's avatar

@Shibbir The $unset is a MongoDB update operator that is used to remove a field from a document. It is used in this code to remove the 'aff_link' and 'availability' fields from a document in the specified MongoDB collection.

The update method in Laravel takes an array of update operators and values, in this case, the $unset operator and the fields to be removed. The $unset operator is followed by an associative array where the keys are the field names to be removed and the values are set to 1. This is the syntax used in MongoDB update operations to remove fields.

You can find more information about the $unset operator in MongoDB documentation: https://docs.mongodb.com/manual/reference/operator/update/unset/

1 like

Please or to participate in this conversation.