首页 > 解决方案 > 如何在 MongoDB 中保存嵌套文档对象

问题描述

我有一个看起来像这样的对象,我想将它保存到我的 MongoDb 数据库中。我在遍历itemRows数组/对象时遇到问题。这是我到目前为止所做的一个示例。

{
   "itemRows": [
      {
        "item_name": "Item 01",
        "item_reg_name": "advjksdnvkj",
        "item_quantity": 2,
        "item_description": "Description",
        "item_region": "CAPE TOWN"
    },
    {
        "item_name": "Item 02",
        "item_reg_name": "98687",
        "item_quantity": 23,
        "item_description": "scscsdcsd",
        "item_region": "JOHANNESBURG"
    }
   ],
  "client_name": "AA Trucks",
  "client_contact_person": "Anele",
  "client_contact_number": "021 000 0000",
  "client_contact_email": "clientemail@gmail.com",
  "client_code": "BHJHJKBHHH",
}

我的储蓄方法

/*
 * ==================================================
 * Post the Sales information capture
 * ==================================================
 */

router.post('sales/', function (req, res, next) {
   var decoded = jwt.decode( req.query.token );

   User.findById( decoded.user._id, function (err, user) {

     if (err) {
        return res.status(500).json({
            title : "Not Authorised !",
            error : err
        });
    }

    var dataCapture = new Sales({
        user: user,

        client_name            : req.body.client_name,
        client_contact_person  : req.body.client_contact_person,
        client_contact_email   : req.body.client_contact_email,
        client_contact_number  : req.body.client_contact_number,

        //How do I save the 'itemRows' array/object ?
    }),
    lineItems = new Items();

    dataCapture.save(function (err, result) {
        if (err) {
             return res.status(500).json({
                 title: 'An error occurred',
                 error: err
             });
        }

        user.save();

        for (var index = 0; index < req.body.itemRows.length; index++) {
            //salesItems
            /*lineItems.save({
                sales_id    : result._id,
                item_name   : req.body.itemRows[index].item_name
            });*/
        }

        res.status(201).json({
             message: 'Data Captured Successfully !',
             obj: result
         });
    });
 });
});

这是我的销售模型Pastebin

问题 !

1.) 如何保存itemRows数组?

保存到 MongoDb 后,我的集合/文档如下所示。

在此处输入图像描述

2.) 如何itemRows与父对象一起显示在表格中?

<table class="table table-hover table-striped">
        <thead>
        <tr>
          <th> Client </th>
          <th> Job Bag : </th>
          <th> Job No : </th>
          <th> Days In Que : </th>
          <th> Exit Date : </th>
          <th> Quantity : </th>
          <th> Actions : </th>
        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let sale of newSales; let i = index">
          <td> {{ sale.client_name }} </td>
          <td> 763951 </td>
          <td> 62965 </td>
          <td> {{ sale.createDate | timeAgo }}</td>
          <td> {{ sale.date_to_be_finished | date:'dd-MM-yyyy' :'+0200'}} </td>
          <td> *<tree-root [nodes]="itemRows" [options]="options"></tree-root> </td>
          <td>
            <button class="btn btn-outline-primary btn-sm" (click)="configureJob()"> Configure </button>
          </td>
        </tr>

        </tbody>
  </table>

*笔记

<tree-root [nodes]="itemRows" [options]="options"></tree-root>

这来自Angular Tree-component。我希望我的问题很清楚。

3.) 我怎么能

为了利益。我的组件看起来像.

我怎样才能绑定[nodes]="itemRows"*ngFor="let sale of newSales; let i = index"循环。

先感谢您。

标签: javascriptnode.jsmongodbexpressmongoose

解决方案


推荐阅读