Union query in mongodb without using map/reduce Union query in mongodb without using map/reduce mongodb mongodb

Union query in mongodb without using map/reduce


I think the ultimate conclusion you will come to is that your schema is incorrect. Put all your objects of the same purpose and shape into a single collection. That's how to work in harmony with mongodb. Making collections by month is a mistake that will cause you endless needless battles with mongo. Once you have everything in one collection as intended, the aggregation can handle grouping by month for you as per the aggregation example of usernames ordered by join month.


read this at first:

http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/

mongo does not have something like union but i think $or will be useful for your needs.

read this answer: https://stackoverflow.com/a/14928646/1321404


Starting Mongo 4.4, the aggregation framework provides a new $unionWith stage, performing the union of two collections (the combined pipeline results from two collections into a single result set):

// collection1://   { "date" : "2013-12-27", "origin" : "collection1" }//   { "date" : "2017-03-11", "origin" : "collection1" }// collection2://   { "date" : "2014-11-02", "origin" : "collection2" }db.collection1.aggregate([  { $unionWith: "collection2" },  { $project: { date: true } }])// { "date" : "2013-12-27" }// { "date" : "2017-03-11" }// { "date" : "2014-11-02" }

Note that you can obtain the same result by first selecting columns and then performing the union (the collection being combined can be applied an aggregation pipeline before the union is performed):

db.collection1.aggregate([  { $project: { date: true } },  { $unionWith: {      coll: "collection2",      pipeline: [{ $project: { date: true } }]  }}])