Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two collection based on id(transectionid) using node.js?

i have transection and purchase collections,which is contaning transection and purchase details now i want to convert it into single collection. based on transectionid we need to combine the documents

Below is my transection collection data's

{

    "transectionid": "1",
    "transectionamount": "2000",
    "transectiondate": "2016-07-12 19:22:28",

},
{

    "transectionid": "2",
    "transectionamount": "1000",
    "transectiondate": "2016-07-12 20:17:11",

}

below is my purchase collection datas

{

    "purchaseid": "1",
    "transectionid": "1",
    "itemprice": "1200",
    "itemcode": "edcvb",
    "itemquantity": "1",


},
{

    "purchaseid": "2",
    "transectionid": "1",
    "itemprice": "800",
    "itemcode": "dfgh",
    "itemquantity": "2",


},
{

    "purchaseid": "3",
    "transectionid": "2",
    "itemprice": "1000",
    "itemcode": "zxcvb",
    "itemquantity": "1",


}

my expectation result : this is how order collecion should store {

        "transectionid" : "1",
        "transectionamount": "2000",
        "transectiondate": "2016-07-12 19:22:28",
        "items" : [
                {
                    "purchaseid": "1",
                    "itemprice":"1200",
                    "itemcode": "edcvb",
                    "itemquantity": "1",
                },
                {
                    "purchaseid": "2",
                    "itemprice": "800",
                    "itemcode": "dfgh",
                    "itemquantity": "2",
                }
        ]
}
{

        "transectionid" : "2",
        "transectionamount": "1000",
        "transectiondate": "2016-07-12 20:17:11",
        "items" : [
                {
                    "purchaseid": "3",
                    "itemprice":"1000",
                    "itemcode": "zxcvb",
                    "itemquantity": "1",
                }
        ]
}
like image 549
its me Avatar asked Dec 02 '16 12:12

its me


2 Answers

well using javascript and just some for cycles you can do this.

var transactionsSize = transactions.length;
var purchasesSize = purchases.length
var finalArray = JSON.parse(JSON.stringify(transactions));

for(var i = 0 ; i < transactionsSize; i++ ) {
    for(var j = 0; j < purchasesSize; j++) {
        if(transactions[i].transactionid == purchases[j].transactionid) {
            if(!finalArray[i].items) {
                finalArray[i].items = [];
                finalArray[i].items.push(purchases[j]);
            } else {
                finalArray[i].items.push(purchases[j]);
            }
         }
     }
}

here is a working fiddle(note that some names are not the same as your input'transections' for example) https://jsfiddle.net/yph9yc86/1/

like image 109
rule Avatar answered Oct 12 '22 00:10

rule


If you are using native mongo driver, you need to implement it yourself.

If you are using mongoose, take a look at population, and even cross DB population, as they already did all the work.

For example, according to your schemas:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema

var transactionSchema = Schema({
  transectiondate: Date,
  transectionid : Number,
  transectionamount: Number,
  items: [{ type: Number, ref: 'Item' }]
});

var itemSchema = Schema({
  transectionid: Number,
  purchaseid : Number,
  itemprice: Number,
  itemcode: String,
  itemquantity: Number,
});

var Transaction = mongoose.model('Transaction', transactionSchema);
var Item = mongoose.model('Item', itemSchema);

Transaction
.find({})
.populate('items')
.exec(function (err, transactinos) {
  console.log(transactinos);
});
like image 23
Ron Dadon Avatar answered Oct 11 '22 23:10

Ron Dadon