简体   繁体   English

结合来自两个不同模型 MongoDB 的两个不同查询的结果

[英]Combining result of two different Queries from two different Model MongoDB

So first I have a query that finds books that has been borrowed by user, it will search using the Borrow model所以首先我有一个查询来查找用户借过的书,它将使用 Borrow 模型进行搜索

const bookqueryinitial = await Borrow.find({borrower_Id : String(_id), borrowStatus: req.query.status}).sort({"borrowDate": -1 }).skip(skip).limit(pageSize);

it will return results like this它会返回这样的结果

[
   {
     _id: new ObjectId("628ebcc10944a1223397b057"),
     borrower_Id: '6278d1b6b4b7659470572e19',
     borrowedbook_Id: '62710ac63ad1bfc6d1703162',
     borrowStatus: 'pending',
     borrowDate: 2022-05-25T23:33:21.849Z,
     __v: 0
   },
   {
     _id: new ObjectId("628d9c0b9a3dc72f4aa72f1a"),
     borrower_Id: '6278d1b6b4b7659470572e19',
     borrowedbook_Id: '62710ac63ad1bfc6d170314d',
     borrowStatus: 'pending',
     borrowDate: 2022-05-25T03:01:31.416Z,
    __v: 0
    }
 ]

next is I will map through the borrowedbook_Ids of the result and store them in an array接下来是我将映射结果的borrowedbook_Ids并将它们存储在一个数组中

const booksinsidequery = bookqueryinitial.map(bookids=>{
      return bookids.borrowedbook_Id
    })

then I will search the ids that is stored in array and search for those ids in the Book model然后我将搜索存储在数组中的 id 并在 Book 模型中搜索这些 id

 const bookquery = await Book.find({ '_id': { $in: booksinsidequery } });

\\and the result is somethign like this

 [
   {
    _id: new ObjectId("62710ac63ad1bfc6d170314d"),
     title: "Girl who kicked the Hornet's Nest",
     author: 'Larsson, Steig',
     genre: [ 'fiction' ],
     publisher: '',
     dateOfPublication: 2017-10-25T00:00:00.000Z,
     noOfCopies: 14,
     type: 'Article',
     form: 'Fiction',
     isbn: '978-69793-4824559-56755-9',
     dateAdded: 2003-04-23T00:00:00.000Z,
     noOfBookmarks: [ [Object] ],
     noOfLikes: [],

   },
   {
     _id: new ObjectId("62710ac63ad1bfc6d1703162"),
     title: 'We the Nation',
     author: 'Palkhivala',
     genre: [ 'philosophy' ],
     publisher: '',
     dateOfPublication: 2011-11-22T00:00:00.000Z,
     noOfCopies: 94,
     type: 'Book',
     form: 'Non-fiction',
     isbn: '978-65685-4156343-802140-8',
     dateAdded: 2010-06-08T00:00:00.000Z,
     noOfLikes: [],
     noOfBookmarks: []
   }
 ]

Now before sending the result of the query to the client side, I want to bind my initial queries from Borrow model to my Book model and the final result should be like this现在在将查询结果发送到客户端之前,我想将我的初始查询从 Borrow 模型绑定到我的 Book 模型,最终结果应该是这样的

 [
   {
    _id: new ObjectId("62710ac63ad1bfc6d170314d"),
     title: "Girl who kicked the Hornet's Nest",
     author: 'Larsson, Steig',
     genre: [ 'fiction' ],
     publisher: '',
     dateOfPublication: 2017-10-25T00:00:00.000Z,
     noOfCopies: 14,
     type: 'Article',
     form: 'Fiction',
     isbn: '978-69793-4824559-56755-9',
     dateAdded: 2003-04-23T00:00:00.000Z,
     noOfBookmarks: [ [Object] ],
     noOfLikes: [],

     //added properties based on matched condition  (Borrow.borrowedbook_Id === Book._id)
     borrowStatus: 'pending',
     borrowDate: 2022-05-25T03:01:31.416Z,
   },
   {
     _id: new ObjectId("62710ac63ad1bfc6d1703162"),
     title: 'We the Nation',
     author: 'Palkhivala',
     genre: [ 'philosophy' ],
     publisher: '',
     dateOfPublication: 2011-11-22T00:00:00.000Z,
     noOfCopies: 94,
     type: 'Book',
     form: 'Non-fiction',
     isbn: '978-65685-4156343-802140-8',
     dateAdded: 2010-06-08T00:00:00.000Z,
     noOfLikes: [],
     noOfBookmarks: [],
     
   //added properties based on matched condition  (Borrow.borrowedbook_Id === Book._id)
     borrowStatus: 'pending',
     borrowDate: 2022-05-25T23:33:21.849Z,
   }
 ]

How can I attain these results?我怎样才能获得这些结果?

You can achieve this with the aggregation framework that MongoDB provides.您可以使用 MongoDB 提供的聚合框架来实现这一点。

Based on your data here is an example: https://mongoplayground.net/p/DQJIbcqBDKM根据您的数据,这里是一个示例: https ://mongoplayground.net/p/DQJIbcqBDKM

  • Lookup : the operator helps you to join data between different collections and the matched data will be in an array. Lookup :运算符帮助您连接不同集合之间的数据,匹配的数据将在一个数组中。 In this case, called borrows在这种情况下,称为借用

  • Unwind : will create a document per item on a specified array Unwind :将为指定数组上的每个项目创建一个文档

  • Addfields : Allows you to create new attributes, in this case, the two that you wanted "borrowStatus" and "borrowDate" Addfields :允许您创建新属性,在这种情况下,您想要的两个属性“borrowStatus”和“borrowDate”

  • Project : this operator allows you to hide or show data in the next stage. Project :此操作符允许您在下一阶段隐藏或显示数据。 In this case, 0 means that we will hide a specific attribute在这种情况下,0 意味着我们将隐藏一个特定的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM