<pre><code>//you can now use a new feature in Mongo 4.4.1 to combine documents in two collections
//Working code
//query data in collection as given in problem statement
> db.colA.find();
{ "_id" : ObjectId("507f1f77bcf86cd799439011"), "species" : "Falcon", "beak_type" : "hooked", "hates_fish" : "True" }
> db.colB.find();
{ "_id" : ObjectId("507f1f77bcf86cd799439012"), "species" : "Haddock", "hates_fish" : "True" }
//use aggregate and $unionWith to acheive your goal
> db.colA.aggregate([
... {$project:{species:1,hates_fish:1,_id:0}},
... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}}
... ]);
{ "species" : "Falcon", "hates_fish" : "True" }
{ "species" : "Haddock", "hates_fish" : "True" }
> print("MongoDB",db.version());
MongoDB 4.4.1
>
//if you currently don't have 4.4.1 and you want to use this feature for your task,
//please upgrade to Mongo DB 4.4.1, sooner or later you need to upgrade!
//you can further add sort and skip commands in the same aggregate query
//example below:
> db.colA.aggregate([
... {$project:{species:1,hates_fish:1,_id:0}},
... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}},
... {$sort:{species:-1}}
... ]);
{ "species" : "Haddock", "hates_fish" : "True" }
{ "species" : "Falcon", "hates_fish" : "True" }
> db.colA.aggregate([
... {$project:{species:1,hates_fish:1,_id:0}},
... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}},
... {$sort:{species:-1}},
... {$skip:1}
... ]);
{ "species" : "Falcon", "hates_fish" : "True" }
>
</code></pre>