簡體   English   中英

如何在mongodb中加入三個collections?

[英]How to join three collections in mongodb?

我正在使用幾乎純 JS 代碼在 MongoDB 中加入三個 collections,我相信必須有一種更簡單的方法來做到這一點,只需使用 mongoose 查詢。

這是我的情況; 我有一個可以創建和銷售文件的賣家。 我希望我的賣家能夠看到他出售的文件。

這是我的 Order 集合架構:

 const OrderSchema = new mongoose.Schema({ //... user: { type: mongoose.Schema.ObjectId, ref: 'User', required: true }, documents: [ { document: { type: mongoose.Schema.ObjectId, ref: 'Document', required: true }, price: { type: Number, required: [true, 'Price is required'] } } ], //... });

我的文檔架構如下所示:

 const DocumentSchema = new mongoose.Schema({ //... title: { type: String, required: [true, 'Please provide a title'], }, author: { type: mongoose.Schema.ObjectId, ref: 'Seller', required: true }, //... });

正如我之前所說,我想獲得一份屬於已售出的已登錄賣家的文檔列表。 (像這樣)。

 "data": [ { "orderId": "606b448dd2d9d643a811bc33", "documentId": "606b448dd2d9d643a811bc34", "title": "Document 1 u1", "price": 90, "createdAt": "2021-04-05T17:10:37.469Z" }, { "orderId": "606b43d2cd9b2740b8b8974b", "documentId": "606b43d2cd9b2740b8b8974c", "title": "Business Affiliate u7", "price": 222, "createdAt": "2021-04-05T17:07:30.859Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bef", "title": "Business Affiliate u7", "price": 777, "createdAt": "2021-04-05T14:09:07.539Z" }, { "orderId": "606b1a03ec048e0d44cb3bee", "documentId": "606b1a03ec048e0d44cb3bf0", "title": "Doc 1 u1", "price": 1, "createdAt": "2021-04-05T14:09:07.539Z" }, ]

我到目前為止得到的代碼是下面的代碼。 它看起來有點丑,它包含三個 Fors。

 const documents = await Document.find().where('author', req.uid).select('orders').populate({ path: 'orders' }) let filteredOrders = []; for (const document of documents) { //GET ALL ORDERS THAT CONTAINS THIS DOCUMENTS const orders = await Order.find().where('documents.document', document._id).populate({ path: 'documents.document', select: 'title' }).sort('-createdAt'); for (const order of orders) { for (const doc of order.documents) { if (doc.document._id.toString() == document._id.toString()) { filteredOrders.push({ orderId: order._id, documentId: doc._id, title: doc.document.title, price: doc.price, createdAt: order.createdAt }); } } } } filteredOrders.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)); res.status(200).json({ success: true, data: filteredOrders });

有沒有辦法只使用 ZCCADDEDB567ABAE643E15DCF0974E503Z 可以獲得相同的結果?

經過一些研究和閱讀文檔(20-ich 選項卡),這是最終代碼(可能需要一些清理):

 let id = mongoose.Types.ObjectId(req.uid); const orders = await Order.aggregate([ { $unwind: '$documents' }, { $lookup: { from: "documents", localField: "documents.document", foreignField: "_id", as: "document" } }, { $match: { 'document.author': id } }, { $project: { user: 1, deliveryEmail: 1, createdAt: 1, documentTitle: '$document.title', documentId: '$documents.document', documentPrice: '$documents.price' } }, { $sort: { 'createdAt': -1 } } ]) res.status(200).json({ success: true, data: orders });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM