使用聚合mongodb mongoose将集合子文档与其他集合子文档连接
所以,我有这种模型使用聚合mongodb mongoose将集合子文档与其他集合子文档连接,mongodb,mongoose,aggregate,mongoose-schema,mongoose-populate,Mongodb,Mongoose,Aggregate,Mongoose Schema,Mongoose Populate,所以,我有这种模型 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.Sche
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.jenis
和kategori.jenis
,但正如您所知,mongoose无法在子文档之间填充,我尝试了toko.find().populate(“produk.jenis”,“label”)
但它显示的错误模式尚未注册到模型“kategori.jenis”。使用mongoose.model(名称、模式)
有任何查询建议吗?我试过了
{
$lookup: {
"from": "kategoris",
"localField": "produk.jenis",
"foreignField": "jenis",
"as": "jenisnya"
}
}
但它似乎不起作用,而是返回一个空数组。我该怎么办?我应该重新安排我的模式吗?你可以试试这个
您的条件$match
解构$unwind
数组produk
带管道$lookup
解构$unwind
数组jenis
match$match
jenis.\u id
仅显示$project
和\u id
标签
在路径中解构$unwind
produk.jenisnya
by\u id并推入$group
produk
@Turivshal我已经做了,你可以在这里看到完整的代码@Turivshal确定这里是@Turivshal或者这里是简单的数据一声你好,@Turivshal感谢你的辛勤工作,非常感谢,查询运行顺利,但我认为
jenisnya
显示了所有数据,如何指定它?我只想获得jenisnya.jenis.\u id
和jenisnya.jenis.label
,我应该将其添加到$project
中吗?
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" }
}
}
])