MongoDB, bao gồm nhiều công cụ nâng cao, là một trong những dịch vụ quản lý cơ sở dữ liệu phổ biến nhất hiện nay. Nhà phát triển không bao giờ dễ dàng ghi nhớ mọi toán tử và tính năng của nó, bất kể họ có thể thông minh đến đâu. Thực tế là, việc một nhà phát triển làm điều này ít phổ biến hơn nhiều so với những gì người ta có thể tưởng tượng
Một ứng dụng kết hợp một hệ thống cơ sở dữ liệu mạnh mẽ như vậy có khả năng bao gồm các tính năng và khả năng rộng rãi. Ngoài ra, các tính năng bảo mật nâng cao có thể được triển khai. Tôi sẽ giải thích cách giải cấu trúc mảng bằng toán tử $unwind trong MongoDB trong bài viết hôm nay
Với MongoDB, bạn có thể chọn từ một loạt các toán tử và tính năng. MongoDB cho phép bạn xây dựng các ứng dụng có thể làm được nhiều hơn các chức năng CRUD. Điều này làm cho MongoDB trở thành cơ sở dữ liệu lý tưởng để xây dựng các ứng dụng tùy chỉnh. Sự phổ biến của nó bắt nguồn từ tính linh hoạt và sức mạnh của nó
Cơ sở dữ liệu cung cấp nhiều toán tử ngoài truy vấn, in và thay đổi dữ liệu. Bất kể mức độ trải nghiệm mà người dùng sở hữu, MongoDB vẫn có thứ gì đó để cung cấp
Hôm nay, tôi sẽ thảo luận về toán tử $unwind
Toán tử $unwind giúp giải cấu trúc các giá trị trường mảng. Một tài liệu cho mỗi phần tử được lấy từ một trường mảng trong các tài liệu đầu vào. Về cơ bản, các phần tử của mảng được thay thế như một phần của tài liệu đầu ra.
Hướng dẫn này sẽ hướng dẫn qua một số ví dụ để bạn có thể hiểu cách hoạt động của toán tử $unwind trong MongoDB. Vì vậy, hãy bắt đầu với hướng dẫn
Cú pháp toán tử $unwind
Đây là giao diện của cú pháp toán tử $unwind
{ $unwind: { path: field path, includeArrayIndex: string, preserveNullAndEmptyArrays: boolean } }
Sử dụng Toán tử $unwind trong MongoDB
Chúng ta hãy bắt đầu sử dụng toán tử $unwind trong các ví dụ khác nhau
Tháo một trường mảng bằng cách sử dụng toán tử $unwind
Trong ví dụ này, chúng ta sẽ sử dụng toán tử $unwind để giải phóng một trường mảng trong MongoDB
- Khởi động dịch vụ MongoDB
- Chọn cơ sở dữ liệu bạn muốn chọn và điều hướng đến nó
show dbs use dronesVerse
- Xem nhanh tất cả các tài liệu trong bộ sưu tập máy bay không người lái. Chúng tôi sẽ in đầu ra với bản in đẹp
> db.drones.find[{}].pretty[] { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : [ "Videography", "Combat", "Rescue", "Construction" ], "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : [ "Photography", "Videography", "Combat", "Rescue", "Construction" ], "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : [ "Photography", "Videography", "Combat", "Rescue", "Construction" ], "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : [ "Videography", "Combat", "Rescue", "Construction" ], "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : [ "Videography", "Combat", "Rescue", "Construction" ], "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } }
- Bây giờ chúng ta hãy sử dụng toán tử $unwind để giải cấu trúc trường mảng “tiện ích”
> db.drones.aggregate[ [ { $unwind: "$utility" } ] ] OR > db.drones.aggregate[ [ { $unwind: { path: "$utility" } } ] ].pretty[]
Đầu ra trông khá lớn
{ "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Videography", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Combat", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Rescue", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Construction", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Photography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Videography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Combat", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Rescue", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Construction", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Photography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Videography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Combat", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Rescue", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Construction", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Videography", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Combat", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Rescue", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Construction", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Videography", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Combat", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } } Type "it" for more > it { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Rescue", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Construction", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } }
Như bạn có thể thấy, chúng ta đã giải cấu trúc thành công trường mảng tiện ích bằng cách sử dụng toán tử $unwind
Tháo một trường mảng bằng toán tử $unwind với tùy chọn includeArrayIndex
Hãy sử dụng tùy chọn includeArrayIndex trong ví dụ này
> db.drones.aggregate[ [ .. { .. $unwind: .. { .. path: "$utility", .. includeArrayIndex: "arrayNumber" .. } .. }]]
- Kiểm tra đầu ra
{ "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Videography", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong[0] } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Combat", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong[1] } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Rescue", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong[2] } { "_id" : ObjectId["615dfce7f1ce6040485ecf62"], "utility" : "Construction", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong[3] } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Photography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[0] } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Videography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[1] } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Combat", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[2] } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Rescue", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[3] } { "_id" : ObjectId["615dff74f1ce6040485ecf63"], "utility" : "Construction", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[4] } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Photography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong[0] } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Videography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong[1] } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Combat", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong[2] } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Rescue", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong[3] } { "_id" : ObjectId["615dff94f1ce6040485ecf64"], "utility" : "Construction", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong[4] } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Videography", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong[0] } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Combat", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong[1] } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Rescue", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong[2] } { "_id" : ObjectId["615dffb0f1ce6040485ecf65"], "utility" : "Construction", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong[3] } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Videography", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[0] } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Combat", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[1] } Type "it" for more > it { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Rescue", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[2] } { "_id" : ObjectId["615dffc8f1ce6040485ecf66"], "utility" : "Construction", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong[3] }
Bạn có thể đã nhận thấy trường “arrayNumber” mà chúng tôi đã đặt trong biểu thức. Điều này biểu thị số chỉ mục mảng
Hủy cuộn một trường mảng bằng cách sử dụng toán tử $unwind với tùy chọnserveNullAndEmptyArrays
Chúng ta sẽ sử dụngserveNullAndEmptyArrays trong ví dụ này cho toán tử $unwind
> db.drones.aggregate[ [ .. { $unwind: { path: "$utility", preserveNullAndEmptyArrays: true } } .. ] ]
- Kiểm tra lại cơ sở dữ liệu của chúng tôi
________số 8
Hoàn hảo. Bạn có thể nhận thấy cách một số mảng trống được bảo toàn bởi tùy chọn toán tử $unwind trong ví dụ này
Đọc thêm. Hướng dẫn cơ bản để sử dụng Toán tử addToSet trong MongoDB với Mongoose
Sự kết luận
Tôi hy vọng rằng bạn đã học cách sử dụng toán tử $unwind trong MongoDB với hướng dẫn đơn giản của chúng tôi. Chúng tôi đang mở rộng bộ sưu tập hướng dẫn của mình và sẽ tiếp tục làm như vậy. Hãy cho chúng tôi biết những gì bạn muốn xem tiếp theo