jonction d'un sous-sous-document de collections avec d'autres sous-documents de collections en utilisant l'agrégat mongodb mongoose

Aug 15 2020

Donc, j'ai ce genre de modèle

const produkSchema = new mongoose.Schema({
    nama_produk: String,
    etalase: {type: mongoose.Schema.Types.ObjectID, ref: 'kategori'},
    kategori: {type: mongoose.Schema.Types.ObjectID, ref: 'kategori'},
    jenis: {type: mongoose.Schema.Types.ObjectID, ref: 'kategori.jenis'},
    bahan: String,
    warna: String,
    deskripsi: String,
    foto_produk: [String],
    harga: Number,
    link_bukalapak: String,
    link_shopee: String,
    link_tokopedia: String,
}, {
    weights: {
        nama_produk: 5,
    },
    timestamps: true
})

const tokoSchema = new mongoose.Schema({
    username: {type: String, trim: true},
    password: {type: String, required: true, select: false},
    merek: String,
    listMerek: [{type: mongoose.Schema.Types.ObjectID, ref: 'produk'}],
    deskripsi: String,
    follower: [{type: mongoose.Schema.Types.ObjectID, ref: 'user'}],
    email: {type: String, trim: true, unique: true},
    instagram: String,
    whatsapp: String,
    website: String,
    alamat: String,
    foto_profil: String,
    bukalapak: String,
    shopee: String,
    tokopedia: String,
    fotoktp: String,
    banner: [{
        gambar: {type: String, required: true, trim: true},
        order: {type: Number, required: true},
    }],
    produk: [produkSchema],
    etalase: [{type: mongoose.Schema.Types.ObjectID, ref: 'kategori'}],
    approve: {type: Number, default: 0}, // 0: pending, 1: reject, 2: approve
    populer: {type: Boolean, default: false},
}, {timestamps: true});

exports.toko = mongoose.model("toko", tokoSchema);

const jenisSchema = new mongoose.Schema({
    label: String,
    gambar: String,
}, {timestamps: true})

const kategoriSchema = new mongoose.Schema({
    label: String,
    gambar: String,
    jenis: [jenisSchema]
}, {timestamps: true});

Donc, ce que je veux rejoindre, c'est, toko.produk.jenisavec kategori.jenis, mais comme vous le savez, la mangouste ne peut pas peupler entre les sous-documents, j'ai essayé toko.find().populate("produk.jenis", "label")mais il affiche une erreur Schema hasn't been registered for model "kategori.jenis". Use mongoose.model(name, schema)des suggestions de requête? J'ai essayé

{
    $lookup: {
           "from": "kategoris",
           "localField": "produk.jenis",
           "foreignField": "jenis",
           "as": "jenisnya"
        }
}

mais cela ne semble pas fonctionner, et renvoyer un tableau vide à la place. Que devrais-je faire? Dois-je réorganiser mon schéma?

Réponses

1 turivishal Aug 15 2020 at 18:59

Tu peux essayer ça,

  • $match vos conditions
  • $unwinddéconstruire le produktableau
  • $lookup avec pipeline
    • $unwinddéconstruire le jenistableau
    • $match rencontre jenis._id
    • $projectpour montrer seulement _idetlabel
  • $unwind déconstruire en chemin produk.jenisnya
  • $group par _id et appuyez sur produk
db.toko.aggregate([
  { $match: { _id: ObjectId("5f1d77aca53cb13980324c73") } }, { $unwind: "$produk" }, { $lookup: {
      from: "kategoris",
      as: "produk.jenisnya",
      let: { pjid: "$produk.jenis" }, pipeline: [ { $unwind: "$jenis" }, { $match: { $expr: { $eq: ["$$pjid", "$jenis._id"] } } },
        { $project: { "jenis._id": 1, "jenis.label": 1 } } ] } }, { $unwind: { path: "$produk.jenisnya" } }, { $group: {
      _id: "$_id", produk: { $push: "$produk" }, // you can add otehr fields as well like alamat alamat: { $first: "$alamat" }
    }
  }
])

Terrain de jeux