Làm cách nào để xóa một đối tượng lồng nhau trong mongodb?

Tôi có một tài liệu như thế này

{'mảng'. [['Bob', true], ['Alice', true], ['Mike', false]]}

Tôi muốn xóa mục chứa 'Bob', sau khi thao tác,
tài liệu sẽ trở thành {'arr'. [['Alice', true], ['Mike', false]]}.

Tôi có thể làm cái này như thế nào?

Hoặc tôi nên chọn một kế hoạch khác?

--
Mã là thơ.

Toán tử

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5 có thể tìm nạp bất kỳ mục hiện có nào từ mảng. Bạn có thể lấy một hoặc nhiều phần tử từ mảng

Bạn có thể xóa (các) đối tượng cụ thể khỏi mảng lồng nhau

Toán tử MongoDB db.cart.updateMany({}, { $pull:{ grocery: "paper", electronics:{ $in:[ "SSD","mouse" ] } } } ) 5

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5 được sử dụng trong MongoDB để xóa các phần tử hiện có khỏi mảng.
db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5 được sử dụng với truy vấn
db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
3

Đây là một ví dụ. Chúng tôi đã chèn như sau

db.cart.insertMany( [
   {
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk", "paper"],
      electronics:["earphone", "bluetooth", "mouse", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water", "paper"],
      electronics:["pendrive", "motherboard", "mouse", "SSD"]
   }
] )

Đây là hai giỏ hàng. Giả sử cửa hàng không có hàng cho

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
4,
db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5 hoặc
db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
6

Vì vậy, bạn muốn xóa các sản phẩm này khỏi giỏ hàng. Sau đó, bạn có thể sử dụng truy vấn sau bằng toán tử

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)

Toán tử

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
8 lấy một mảng để khớp từng phần tử với tài liệu. Sau thao tác này, bộ sưu tập sẽ như sau

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }

Lệnh MongoDB db.cart.updateMany({}, { $pull:{ grocery: "paper", electronics:{ $in:[ "SSD","mouse" ] } } } ) 5 có điều kiện

Giả sử chúng ta có tài liệu sau

________số 8

Bây giờ, bạn muốn loại bỏ các phần tử trong mảng

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
0 có giá trị lớn hơn 50. Sau đó, lệnh sẽ giống như sau

db.cart.insertMany( [
   {
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk", "paper"],
      electronics:["earphone", "bluetooth", "mouse", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water", "paper"],
      electronics:["pendrive", "motherboard", "mouse", "SSD"]
   }
] )
0

Bạn sẽ thấy rằng chúng tôi đã sử dụng

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
1 làm tham số đầu tiên của
{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
2. Điều này là do chúng tôi muốn cập nhật tất cả các yếu tố

Giả sử bạn chỉ muốn cập nhật một số

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
3 cụ thể, thì lệnh sẽ như sau

db.cart.insertMany( [
   {
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk", "paper"],
      electronics:["earphone", "bluetooth", "mouse", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water", "paper"],
      electronics:["pendrive", "motherboard", "mouse", "SSD"]
   }
] )
4

Bạn cũng có thể sử dụng toán tử

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
8 trong
{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
3 để chọn nhiều
{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
3 để cập nhật và sử dụng toán tử
{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
7

Sử dụng db.cart.updateMany({}, { $pull:{ grocery: "paper", electronics:{ $in:[ "SSD","mouse" ] } } } ) 5 trong Mảng lồng nhau

Giả sử bạn có bộ sưu tập tài liệu sau

db.cart.insertMany( [
   {
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk", "paper"],
      electronics:["earphone", "bluetooth", "mouse", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water", "paper"],
      electronics:["pendrive", "motherboard", "mouse", "SSD"]
   }
] )
9

Chúng tôi muốn xóa các mục đó khỏi mảng

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }
9 trong đó
{
    _id: 1,
    price: [ 31, 50, 55, 66, 98, 25 ]
},
{
    _id: 2,
    price: [ 51, 55, 65, 91, 19, 26 ]
},
0 bằng 2 và
{
    _id: 1,
    price: [ 31, 50, 55, 66, 98, 25 ]
},
{
    _id: 2,
    price: [ 51, 55, 65, 91, 19, 26 ]
},
1 lớn hơn bằng 8. Sau đó, truy vấn sẽ là

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
3

Sau thao tác này, bộ sưu tập được cập nhật sẽ trông như thế này

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
4

Ví dụ về mảng lồng nhau này được lấy từ tài liệu chính thức của MongoDB. Tại đây bạn có thể đọc tài liệu đầy đủ và biết thêm về toán tử

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)
5

Làm cách nào để xóa dữ liệu khỏi mảng lồng nhau trong MongoDB?

Vui lòng xem toán tử $pull . $pull sẽ xóa giá trị đã chỉ định khỏi mảng. Trong trường hợp bạn không chắc mình muốn xóa giá trị cụ thể nào khỏi mảng mà chỉ muốn xóa giá trị đầu hoặc cuối trong mảng thì có thể sử dụng toán tử $pop.

Làm cách nào để xóa đối tượng khỏi đối tượng trong MongoDB?

Bạn có thể xóa các bản ghi hoặc tài liệu như nó được gọi trong MongoDB bằng cách sử dụng phương thức remove(). Tham số đầu tiên của phương thức remove() là đối tượng truy vấn xác định tài liệu nào cần xóa. Ghi chú. Phương thức remove() không được dùng nữa. Thay vào đó, hãy sử dụng xóaOne() hoặc xóaMany()

Làm cách nào để xóa mảng lồng nhau trong cầy mangut?

Toán tử $pull loại bỏ khỏi một mảng hiện có tất cả các phiên bản của một giá trị hoặc các giá trị khớp với một điều kiện đã chỉ định

Tôi có thể tìm các tài liệu lồng nhau trong MongoDB ở đâu?

Truy cập tài liệu nhúng/lồng nhau – . using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.