简体   繁体   English

如何在mongodb中加入三个collections?

[英]How to join three collections in mongodb?

I am using almost-pure JS code to join three collections in MongoDB, and I believe there must be an easier way to do that using just mongoose query.我正在使用几乎纯 JS 代码在 MongoDB 中加入三个 collections,我相信必须有一种更简单的方法来做到这一点,只需使用 mongoose 查询。

Here is my case;这是我的情况; I have a seller that can create and sell documents.我有一个可以创建和销售文件的卖家。 I want my seller to be able to see the documents that he sold.我希望我的卖家能够看到他出售的文件。

This is my schema for the Order collection:这是我的 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'] } } ], //... });

and my schema for the document looks like this:我的文档架构如下所示:

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

As I said before, I want to get a list of documents that belongs to the logged in Seller that got sold.正如我之前所说,我想获得一份属于已售出的已登录卖家的文档列表。 (Something like this). (像这样)。

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

The code I got this far is the one below.我到目前为止得到的代码是下面的代码。 It looks kinda ugly and it contains three Fors.它看起来有点丑,它包含三个 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 });

Is there a way with just Mongoose that I can get the same result?有没有办法只使用 ZCCADDEDB567ABAE643E15DCF0974E503Z 可以获得相同的结果?

After some research and reading the documentations (20-ich tabs) this is the final code (It may needs some cleanups):经过一些研究和阅读文档(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