使用聚合 mongodb mongoose 将集合子文档与其他集合子文档连接起来 [英] joining collections sub-subdocument with other collections subdocument using aggregate mongodb mongoose

查看:88
本文介绍了使用聚合 mongodb mongoose 将集合子文档与其他集合子文档连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这种模型

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});

所以我想加入的是,toko.produk.jeniskategori.jenis,但如你所知,猫鼬不能在子文档之间填充,我有尝试了 toko.find().populate("produk.jenis", "label") 但它显示错误 Schema 尚未为模型kategori.jenis"注册.使用 mongoose.model(name, schema) 任何查询建议?我试过了

so what I want to join is, toko.produk.jenis with kategori.jenis, but as you know, mongoose can't populate between subdocument, I have tried toko.find().populate("produk.jenis", "label") but it showing error Schema hasn't been registered for model "kategori.jenis". Use mongoose.model(name, schema) any query suggestions? I've tried

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

但它似乎不起作用,而是返回一个空数组.我应该怎么办?我应该重新排列我的架构吗?

but it doesn't seem work, and returning an empty array instead. What should I do? Should I rearrange my schema?

推荐答案

你可以试试这个,

  • $match 你的条件
  • $unwind 解构 produk 数组
  • $lookup 带管道
    • $unwind 解构 jenis 数组
    • $match 匹配 jenis._id
    • $project 只显示 _idlabel
    • $match your conditions
    • $unwind deconstruct produk array
    • $lookup with pipeline
      • $unwind deconstruct jenis array
      • $match match jenis._id
      • $project to show only _id and label
      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" }
          }
        }
      ])
      

      游乐场

      这篇关于使用聚合 mongodb mongoose 将集合子文档与其他集合子文档连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆