Javascript 在mongodb中组合不同的文档

Javascript 在mongodb中组合不同的文档,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我想组合不同的文档,并使用Promissions从mongodb数据库中检索它们 因此,我有一个作者的作者id,并希望显示作者拥有的所有书籍以及它们存储在哪个图书馆,以及它们是否可用。这将在axpress服务器中使用pug呈现页面 我首先查找作者,然后我想浏览图书馆并搜索所有可用的书籍,最后如果该书的作者与我们搜索的作者相同,我想显示它是否在找到它的图书馆中找到,以及它是否在可用或不可用列表中找到 var AuthorSchema = new Schema( { f

我想组合不同的文档,并使用Promissions从mongodb数据库中检索它们

因此,我有一个作者的作者id,并希望显示作者拥有的所有书籍以及它们存储在哪个图书馆,以及它们是否可用。这将在axpress服务器中使用pug呈现页面

我首先查找作者,然后我想浏览图书馆并搜索所有可用的书籍,最后如果该书的作者与我们搜索的作者相同,我想显示它是否在找到它的图书馆中找到,以及它是否在可用或不可用列表中找到

   var AuthorSchema = new Schema(
    {
        first_name: {type: String, required: true, max: 100},
        family_name: {type: String, required: true, max: 100},
        date_of_birth: {type: Date},
        date_of_death: {type: Date},
    });

    var BookSchema = new Schema(
    {
        title: {type: String, required: true},
        author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
        summary: {type: String, required: true},
        isbn: {type: String, required: true},
        genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
    });
    var library = new Schema({

        name:{type:String,required:true},
        available_books:[{type:Schema.Types.ObjectId,ref:'Book'}],
        unavailable_books:[{type:Schema.Types.ObjectId,ref:'Book'}]
    });

//this is the code I came up with,
new Promise((resolve,reject)=>{
  author.findById(id).exec(function(err,author){
  if(err) reject(err)
  else resolve(user)

  }
})
.then(author => {
// stuck here, probs search all the library their available books eend for each book check if the author is the same. 




}

我如何将图书与它们所在的图书馆名称以及它们是否可用进行映射,因此,将图书名称、图书馆名称以及它们是否可用放在3个列旁边。

解决此问题的主要方法有两种:

使用聚合框架的$lookup让mongo将这些集合合并为一个结果: 使用应用程序端代码组合集合这是PSEDOOCODE: const books=等待books.findauthorId; const bookObjectIds=books.map{{u id}=>\u id; const libraries=wait Library.find{$or:[ {可用图书:{$in:BookObjectId}, {unavailable_books:{$in:bookObjectIds}}, ]}; …将书籍和图书馆合并为您想要的格式