SQL 'UNION ALL' like implementation in MongoDB

前端 未结 3 1873
慢半拍i
慢半拍i 2021-01-23 01:53

There are two collections:

Sales

{
  \"_id\" : ObjectId(\"5ba0bfb8d1acdc0de716e839\"),
  \"invoiceNumber\" : 1,
  \"saleDate\" : ISODate(\"2018-09-01T00:         


        
相关标签:
3条回答
  • 2021-01-23 02:22

    You can try below aggregation

    db.sales.aggregate([
      { "$limit": 1 },
      { "$facet": {
        "collection1": [
          { "$limit": 1 },
          { "$lookup": {
            "from": "sales",
            "pipeline": [
              { "$match": {
                "date": { "$gte": ISODate("2018-09-01"), "$lte": ISODate("2018-09-10") },
                "customer.name": customerName
              }},
              { "$project": {
                "_id":0, "dated": "$saleDate", "heading": "Sale", "particulars": "$invoiceNumber",
                "amount": "$totalAmount", "modeOfPayment": null
              }}
            ],
            "as": "collection1"
          }}
        ],
        "collection2": [
          { "$limit": 1 },
          { "$lookup": {
            "from": "transactions",
            "pipeline": [
              { "$match": {
                "transactionDate": { "$gte": ISODate("2018-09-01"), "$lte": ISODate("2018-09-10") },
                "userId": userId, "partyName": customerName
              }},
              { "$project": {
                "_id":0, "dated": "$transactionDate", "heading": "Payment","particulars": "$transactionNumber",
                "amount": "$amount", "paymentMode": "$transactionMode"
              }}
            ],
            "as": "collection2"
          }}
        ]
      }},
      { "$project": {
        "data": {
          "$concatArrays": [
            { "$arrayElemAt": ["$collection1.collection1", 0] },
            { "$arrayElemAt": ["$collection2.collection2", 0] },
          ]
        }
      }},
      { "$unwind": "$data" },
      { "$replaceRoot": { "newRoot": "$data" } },
      { "$sort": { "dated": -1 }}
    ])
    
    0 讨论(0)
  • 2021-01-23 02:29

    Disclaimer: The technique presented below is not exactly advisable. ;) This is true in particular when dealing with big collections. However, it can be used to achieve the identical effect as a SQL UNION ALL from MongoDB v3.6 onwards.

    Given a collection first and a collection second:

    db.first.aggregate([{
        $group: { // create an array to hold all documents of the first collection
            "_id": null,
            "first": {
                $push: "$$ROOT"
            }
        }
    }, {
        $lookup: { // perform some kind of ridiculous lookup which will return all documents from the second collection in an array
            from: "second",
            let: { /* we do not need any variables */ },
            pipeline: [ { $match: { /* this filter will match every document */ } } ],
            as: "second"
        }
    }, {
        $project: {
            "all": { $concatArrays: [ "$first", "$second" ] } // merge the two collections
        }
    }, {
        $unwind: "$all" // flatten the resulting array
    }, {
        $replaceRoot: { "newRoot": "$all" } // move content of "all" field all the way up 
    }], {
        allowDiskUse: true // make sure we do not run into memory issues
    })
    
    0 讨论(0)
  • 2021-01-23 02:42

    I dont think so that you can perform a union on 2 different collections in mongodb.

    However you can get data from both collections using q.all and afterwards make a union from them using your own function or may be third party module like lodash.

    The only way you can perform union in mongodb is as mentioned here. https://docs.mongodb.com/manual/reference/operator/aggregation/setUnion/

    0 讨论(0)
提交回复
热议问题