如何在MongoDB中保存嵌套文档对象

时间:2018-04-30 09:27:39

标签: javascript node.js mongodb express mongoose

我有一个看起来像这样的对象,我想保存到我的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后,我的收藏/文档看起来像这样。

enter image description here

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.。我怎么能

为了兴趣。 My Component looks like

如何将[nodes]="itemRows"绑定到*ngFor="let sale of newSales; let i = index"循环。

提前谢谢。

0 个答案:

没有答案