【发布时间】:2021-09-17 02:50:23
【问题描述】:
我使用 Mongodb 作为我的数据库来更新变量的值。我正在使用 node.js 和 express 服务器。
这是我的代码:
router.route("/update/").put( async (req, res) =>{
const count_update = {
date: new Date(),
count: req.body.count
}
var searchDate = count_update.date.toISOString().slice(0,10);
const auth = req.currentUser;
if (auth) {
try{
await User.updateOne(
{ id: auth.uid },
[{
$set: {
count: {
$cond: [
{
$gt: [searchDate, { $substr: [{ $last: "$count.date" }, 0, 10] }]
},
{
$concatArrays: [
"$count",
[{ date: count_update.date, count: count_update.count }]
]
},
{
$map: {
input: "$count",
in: {
$mergeObjects: [
"$$this",
{
$cond: [
{
$eq: ["$$this.date", { $last: "$count.date" }]
},
{ count: count_update.count },
{}
]
}
]
}
}
}
]
}
}
}]
)
此代码仅更新变量 6 次。然后我必须重新加载应用才能再更新 6 次。
可能是什么问题?
【问题讨论】:
-
req.body.count 的值是多少?
-
没有固定值,变化很快
标签: node.js arrays mongodb mongoose mongodb-query