從 MongoDB 中的類矩陣文件中刪除值


若要從矩陣中刪除值,請在 MongoDB 中使用$pull。我們建立一個包含文件的集合 −

> db.demo632.insertOne(
...    {
...       "arrayMatrix": [
...          [10,20],
...          [10,20],
...          [10,20],
...          [10,20],
...          [10,20],
...          [10,20]
...       ]
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e9b27b46c954c74be91e6c0")
}

在集合中顯示所有文件,藉助 find() 方法 −

> db.demo632.find().pretty();

這將產生以下輸出 −

{
   "_id" : ObjectId("5e9b27b46c954c74be91e6c0"),
   "arrayMatrix" : [
      [
         10,
         20
      ],
      [
         10,
         20
      ],
      [
         10,
         20
      ],
      [
         10,
         20
      ],
      [
         10,
         20
      ],
      [
         10,
         20
      ]
   ]
}

以下是從 MongoDB 中的類矩陣文件中刪除值的查詢 −

> db.demo632.update(
...    {},
...    {
...       "$pull": {
...          "arrayMatrix.0": 20,
...          "arrayMatrix.1": 20,
...          "arrayMatrix.2": 20,
...          "arrayMatrix.3": 20,
...          "arrayMatrix.4": 20,
...          "arrayMatrix.5": 20
...       }
...    }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

在集合中顯示所有文件,藉助 find() 方法 −

> db.demo632.find().pretty();

這將產生以下輸出 −

{
   "_id" : ObjectId("5e9b27b46c954c74be91e6c0"),
   "arrayMatrix" : [
      [
         10
      ],
      [
         10
      ],
      [
         10
      ],
      [
         10
      ],
      [
         10
      ],
      [
         10
      ]
   ]
}

更新日期:2020 年 5 月 12 日

92 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.