Database

db={"library":[{"id":1,"name":"Library 1","unavailable_books":[1,3,4]},{"id":2,"name":"Library 2","unavailable_books":[]}],"books":[{"id":1,"title":"Hamlet","author":"William Shakespeare"},{"id":2,"title":"Lord of the ring","author":"J.R.R Tolkien"},{"id":3,"title":"1984","author":"George Orwell"},{"id":4,"title":"Don Quichotte","author":"Miguel de Cervantes"},{"id":5,"title":"Moby Dick","author":"Herman Melville"},{"id":6,"title":"Dr. Jekyll and M. Hyde","author":"Robert Louis Stevenson"}]}

Query

/**To get everything*/db.library.aggregate([{$match:{id:2}},{$lookup:{from:"books",localField:"unavailable_books",foreignField:"id",as:"unavailableBooks"}},{$group:{_id:"$id",unavailable:{$push:{id:"$unavailableBooks.id",firstName:"$unavailableBooks.title"}}}}])

Result