我正在学习nodeJS,并且正在开发一个预算应用程序,其中使用mongodb存储数据。
我正在尝试基于Node + Express + Mongo创建一个CRUD应用程序。我觉得我设计的方案对于该项目不是一个好的解决方案。我想将所有transactionInfo保留在一个位置,并将其链接到statmentInfo,这样我就可以轻松地为该节(blingBlaw.transactionInfo)
编制索引或进行修改。
我也尝试以这种方式创建模式-但遇到了类似的问题。
blingBlaw.statmentData[0].statmetInfo = [{}]
blingBlaw.statmentData[0].transactionInfo = [{}]
我觉得这是不允许我更新嵌套的transactionInfo的问题
这是mongo查询的图片,需要使用此方法来修改带点符号的嵌套集合
这是我得到的消息,我不确定这个错误是否意味着,我遍历数组以修改不匹配项或其他具有我给出的值的方式。
{ n: 0, nModified: 0, ok: 1 }
我想出一个添加,修改和删除嵌套文档的解决方案。可能是我的设计难以遍历对象数组。
我正在尝试存储CSV中的数据->将其转换为数组,然后将其插入 transactionInfo 。
这就是我添加 transactionInfo 的方式(添加就可以了)
// Upload object - setting up for Statement and transaction
let statementInfo = {
statement_id: req.session.statementInfo.statement_id,
statementName: req.session.statementInfo.statementName,
statementType: req.session.statementInfo.statementType,
statementDate: req.session.statementInfo.statementDate,
statementDesc: req.session.statementInfo.statementDesc,
statementFileInfo: req.session.statementInfo.statementFileInfo,
statementCreated: req.session.statementInfo.statementCreated,
statementModified: req.session.statementInfo.statementModified,
statementModifiedtUser: req.session.statementInfo.statementModifiedtUser,
}
let transactionInfo = {};
for (let i in req.body.modulesTransId) {
transactionInfo[i] = {
transactionId: ObjectId(),
transDate: req.body.modulesTransDate[i],
transDesc: req.body.modulesTransDesc[i],
transWithdraw: req.body.modulesTransWithdraw[i],
transDeposite: req.body.modulesTransDeposite[i],
transBalance: req.body.modulesTransBalance[i],
transCat: req.body.modulesCatName[i],
transComment: req.body.modulesTransComment[i],
transType: req.body.modulesTransType[i],
statementId: req.session.statementInfo.statement_id,
transactiontModified: "",
transactiontModifiedUser: ""
}
};
// set validation Data
let valData = {
"_id": req.session.userId
}
// request DB conections
const db = req.db;
const collectionBling = db.get(collectionBlingBlaw);
// mongo push the new category
collectionBling.update(valData, {
$push: {
"statementInfo": statementInfo,
"transactionInfo": transactionInfo
}
}, {
upsert: true
}, function(err, results) {
if (err) { // If it failed, return error
console.log("err: " + err);
flashData.pageMesage = "Error adding Statement";
flashData.bgColor = "danger";
flashData.info = err;
req.flash('flashData', flashData);
res.redirect('/statement');
} else { // else add category and redirect to Category Dashboard
console.log("Category added: " + results);
flashData.pageMesage = "Statement been added!";
flashData.bgColor = "success";
flashData.info = results;
req.flash('flashData', flashData);
res.redirect('/statement');
}
});
这就是我更新 transactionInfo
的方式const db = req.db;
const collectionBling = db.get(collectionBlingBlaw);
collectionBling.update({
_id: ObjectId(req.session.userId),
"transactionInfo.transactionId": ObjectId(req.body.transTransId),
"transactionInfo.transDate": req.body.transTransDate,
}, {
$set: {
"transactionInfo.$.transCat": req.body.transTransCat,
"transactionInfo.$.transComment": req.body.transTransComment,
"transactionInfo.$.transactiontModified": req.body.transTransComment,
"transactionInfo.$.transactiontModifiedUser": moment().format('MMMM Do YYYY, h:mm:ss a')
}
}, function(err, results) {
if (err) {
flashData.pageMesage = "Error updating" + req.body.transTransDesc;
flashData.bgColor = "danger";
flashData.info = err;
req.flash('flashData', flashData);
res.redirect('/statement');
} else {
console.log(results);
flashData.pageMesage = "Update been good: " + req.body.transTransDesc;
flashData.bgColor = "success";
flashData.info = results;
req.flash('flashData', flashData);
res.redirect('/statement');
}
})
这就是我删除声明的方式
collection.update({
"statementInfo.statementData.statement_id": ObjectId('5be32b29ee4c322140969fd5')
}, {
$pull: {
"statementInfo.$.statementData": {
'statement_id': ObjectId('5be32b29ee4c322140969fd5')
}
}
}, function(err, results) {
if (err) { // if err throw err
console.log("results: " + JSON.stringify(err));
flashData.pageMesage = "Error removing statement" + JSON.stringify(err.message);
flashData.bgColor = "danger";
flashData.info = err;
req.flash('flashData', flashData);
res.redirect('/statement');
}
if (results) {
console.log("results: " + JSON.stringify(results));
flashData.pageMesage = "Statement been removed: " + req.body.statementId;
flashData.bgColor = "success";
flashData.info = results;
req.flash('flashData', flashData);
res.redirect('/statement');
}
});
这是我为该项目设计的方案。
// set newData to insert
const user_id = ObjectId();
const statement_id = ObjectId();
const transactionId_id = ObjectId();
const blingBlaw = {
_id: user_id,
userInfo: {
userId: user_id,
userFannyPack: req.body.fannyPack,
userName: req.body.username,
userPwd: req.body.pwd,
userEmil: "",
userGrup: "",
userCreated: moment().format('MMMM Do YYYY, h:mm:ss a'),
userModify: ""
},
statementInfo: [{
statement_id: statement_id,
statementName: "",
statementType: "",
statementDate: "",
statementDesc: "",
statementFileInfo: "",
statementCreated: moment().format('MMMM Do YYYY, h:mm:ss a'),
statementModified: "",
statementModifiedtUser: ""
}],
transactionInfo: [{
transactionId: transactionId_id,
transactionDate: "",
transactionDesc: "",
transactionWithdraw: "",
transactionDeposite: "",
transactionBalance: "",
transactiontModified: "",
transactiontModifiedUser: "",
statement_id: statement_id
}],
categoryInfo: [{
_id: "",
catName: "",
catParent: "",
catCreate: moment().format('MMMM Do YYYY, h:mm:ss a'),
catModify: ""
}]
}