Mengapa kami menggunakan bersantai di mongodb?

MongoDB, yang mencakup berbagai alat canggih, adalah salah satu layanan manajemen basis data paling populer yang tersedia saat ini. Tidak pernah mudah bagi pengembang untuk mengingat setiap operator dan fiturnya, tidak peduli seberapa pintar dia. Faktanya adalah, jauh lebih jarang bagi pengembang untuk melakukan ini daripada yang dibayangkan

Aplikasi yang menggabungkan sistem basis data yang begitu kuat mampu menyertakan fitur dan kemampuan yang luas. Selain itu, fitur keamanan yang ditingkatkan dapat diimplementasikan. Saya akan menjelaskan cara mendekonstruksi array menggunakan operator $unwind di MongoDB dalam artikel hari ini

Dengan MongoDB, Anda dapat memilih dari berbagai operator dan fitur. MongoDB memungkinkan Anda membangun aplikasi yang dapat melakukan lebih dari sekadar fungsi CRUD. Ini menjadikan MongoDB database yang ideal untuk membangun aplikasi khusus. Popularitasnya berasal dari fleksibilitas dan kekuatannya

Basis data menyediakan berbagai operator selain permintaan, pencetakan, dan perubahan data. Terlepas dari tingkat pengalaman yang dimiliki penggunanya, MongoDB masih memiliki sesuatu untuk ditawarkan

Hari ini, saya akan membahas operator $unwind

Operator $unwind membantu mendekonstruksi nilai bidang array. Dokumen untuk setiap elemen berasal dari bidang array di dokumen masukan. Pada dasarnya, elemen array diganti sebagai bagian dari dokumen keluaran.   

Panduan ini akan membahas beberapa contoh sehingga Anda dapat memahami cara kerja operator $unwind di MongoDB. Jadi, mari kita mulai dengan panduan ini

Sintaks Operator $unwind

Seperti inilah sintaks operator $unwind

{
  $unwind:
    {
      path: field path,
      includeArrayIndex: string,
      preserveNullAndEmptyArrays: boolean
    }
}

Menggunakan Operator $unwind di MongoDB

Mari kita mulai menggunakan operator $unwind dalam contoh yang berbeda

Melepaskan Bidang Array Menggunakan Operator $unwind

Dalam contoh ini, kita akan menggunakan operator $unwind untuk melepas field array di MongoDB

  • Mulai layanan MongoDB
  • Pilih database yang ingin Anda pilih dan arahkan ke dalamnya

show dbs
use dronesVerse
_

  • Melihat sekilas semua dokumen dalam koleksi drone. Kami akan mencetak hasilnya dengan cetakan cantik

> 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"
                ]
        }
}

  • Mari kita gunakan operator $unwind untuk mendekonstruksi bidang array "utilitas".

> db.drones.aggregate( [ { $unwind: "$utility" } ] )
OR
> db.drones.aggregate( [ { $unwind: { path: "$utility" } } ] ).pretty()

Outputnya terlihat cukup besar

{
        "_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"
                ]
        }
}
_

Seperti yang Anda lihat, kami telah berhasil mendekonstruksi bidang larik utilitas menggunakan operator $unwind

Melepaskan Bidang Array Menggunakan Operator $unwind dengan Opsi includeArrayIndex

Mari gunakan opsi includeArrayIndex dalam contoh ini

> db.drones.aggregate( [
..   {
..     $unwind:
..       {
..         path: "$utility",
..         includeArrayIndex: "arrayNumber"
..       }
..    }])

  • Memeriksa keluaran

{ "_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) }
_

Anda mungkin telah memperhatikan bidang "arrayNumber" yang kami atur dalam ekspresi. Ini menunjukkan nomor indeks array

Melepaskan Bidang Array Menggunakan Operator $unwind dengan Opsi preservNullAndEmptyArrays

Kami akan menggunakan preservNullAndEmptyArrays dalam contoh ini untuk operator $unwind

> db.drones.aggregate( [
..    { $unwind: { path: "$utility", preserveNullAndEmptyArrays: true } }
.. ] )

  • Memeriksa database kami lagi

{ "_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" ] } }
_

Sempurna. Anda mungkin telah memperhatikan bagaimana beberapa array kosong dipertahankan oleh opsi operator $unwind dalam contoh ini

Baca selengkapnya. Panduan Utama untuk Menggunakan Operator addToSet di MongoDB dengan Mongoose

Kesimpulan

Saya harap Anda belajar menggunakan operator $unwind di MongoDB dengan tutorial sederhana kami. Kami memperluas koleksi tutorial kami dan akan terus melakukannya. Beri tahu kami apa yang ingin Anda lihat selanjutnya

Bagaimana cara kerja bersantai?

Bagi sebagian orang, pelepasan menjadi apa yang dianggap sebagai pengganti aborsi, di mana alih-alih aborsi anak yang belum lahir, orang tua dapat memilih untuk menggugurkan secara surut, in this case 'unwind', their child at any age from thirteen to seventeen, previously eighteen.

Apa yang bersantai dalam agregasi?

$unwind sangat berguna saat melakukan agregasi. itu memecah dokumen kompleks / bersarang menjadi dokumen sederhana sebelum melakukan berbagai operasi seperti menyortir, mencari, dll

Bagaimana cara menggunakan unset di MongoDB?

Dalam MongoDB, operator $unset digunakan untuk menghapus field tertentu . Nilai yang ditentukan dalam ekspresi $unset tidak berdampak apa pun pada operasi. $unset tidak berpengaruh ketika bidang tidak ada dalam dokumen. nama kolom atau bidang yang akan dihapus.

Bagaimana Anda mengelompokkan setelah bersantai?

The $unwind di MongoDB mendekonstruksi bidang array dari dokumen input untuk menghasilkan dokumen untuk setiap elemen. $group digunakan untuk mengelompokkan dokumen masukan dengan ekspresi _id yang ditentukan dan untuk setiap pengelompokan yang berbeda, menghasilkan dokumen.