javascript - 如何在 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"
循环。
先感谢您。
解决方案
推荐阅读
- python - Python Pandas - 如何为属于同一索引的行赋值?
- python - 有没有办法使用 python 在 Pandas DataFrame 中使用行索引范围选择列的特定部分
- flutter - flutter_datetime_picker RTL 无法正常工作
- java - N叉树的层序遍历的递归
- c# - 如何通过本地方法替换 OrderBy 中的 lambda
- python - 如何根据列值自定义数据框的顺序
- c - 在函数中使用结构变量 - 冲突类型
- r - 根据单元格值 (R) 在矩阵中查找路径
- python - 从类中获取价值到另一个 Python
- javascript - 如何使用 Javascript 滚动到元素?