简体   繁体   中英

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",
                }
        ]
}

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/

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);
});

According to your expected result you may want to get all transaction with populated purchase data as items.

In your structure transectionid is String type and you may not used ref in your schema. so populate transectionid you should use $lookup because we can define from , localField and foreignField . for that you don't need to use ref in your schema.

and you should add items: [] in your Transaction schema because if you didn't add that you may not get items in your result.

in transaction controller can add this function to get all transaction with purchase data as items.

for from field in $lookup you should use lower case and plural form of collection name because when mongoose create collection made it plural. like your exported schema name PurchasesData then it will create like purchasesdatas

like:

exports.getTransactions = function(req,res){ 

  Transection.aggregate([ 
    {$lookup:{from:"purchases", localField:"transectionid", foreignField:"transectionid", as:"items"}} 
  ]).exec(function(err, result) { 
    if(err) { 
      console.log('error============', err); 
      return res.status(400).send("error occurred"); 
    }else{ 
      console.log(result); 
      return res.status(200).send(result); 
    } 
  }); 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM