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

Combining result of two different Queries from two different Model MongoDB

所以首先我有一个查找用户借阅书籍的查询,它将使用借阅模型进行搜索

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

它会return这样的结果

[
   {
     _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
    }
 ]

接下来是我将映射结果的borrowedbook_Ids并将它们存储在数组中

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

然后我将搜索存储在数组中的 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: []
   }
 ]

现在在将查询结果发送到客户端之前,我想将我从 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,
   }
 ]

我怎样才能获得这些结果?

您可以使用 MongoDB 提供的聚合框架来实现这一点。

根据您的数据,这里有一个示例:https://mongoplayground.net/p/DQJIbcqBDKM

  • Lookup :运算符帮助您连接不同集合之间的数据,匹配的数据将在一个数组中。在这种情况下,调用 borrows

  • Unwind : 将为指定数组上的每个项目创建一个文档

  • Addfields :允许您创建新属性,在本例中是您想要的两个“borrowStatus”和“borrowDate”

  • Project :此运算符允许您隐藏或显示下一阶段的数据。在这种情况下,0 表示我们将隐藏特定属性