如何使用node.js组合基于id(transectionid)的两个集合? [英] How to combine two collection based on id(transectionid) using node.js?

查看:45
本文介绍了如何使用node.js组合基于id(transectionid)的两个集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有横切和购买集合,这是现在我要将横断面和购买细节转换成单个集合。
基于transectionid我们需要合并文件

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


推荐答案

根据您的预期结果,您可能希望将填充的购买数据作为项目进行所有交易。

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

在您的结构中 transectionid 字符串类型,你可能没有在你的 ref 中使用架构。所以填充 transectionid 你应该使用 $ lookup ,因为我们可以从 c>, localField foreignField 。为此,您不需要在架构中使用 ref

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.

来自字段 $ lookup 你应该使用小写和复数形式的集合名称,因为当mongoose创建集合使它复数。就像您导出的模式名称 PurchasesData 那样它会创建类似 purchasesdatas

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

喜欢:

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

这篇关于如何使用node.js组合基于id(transectionid)的两个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆