{ To-6ren">
gpt4 book ai didi

node.js - Nodejs中的"BadValue Unsupported projection option"错误

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:32 24 4
gpt4 key购买 nike

MongoError:无法规范化查询:BadValue 不支持的投影选项:$inc:{ count:1 }

var totalActiveUser = (req, res, next) => {
TotalUser.findOne({ default: "active"}, { $inc: { count: 1 }},
function(err, result) {
if (err) throw err;
console.log(result);
res.send(result);
});

模型架构:

   activeCount: {
type: Number,
count: 0,
default:"active"
},

最佳答案

您正在使用 $inc,它是 findOne 的更新操作,它应该与 findOneandupdate 一起使用。借助 findOne,第二个 {} 就变成了投影。

此外,您提出此查询的动机是什么?

如果您想更新:

var totalActiveUser = (req, res, next) => {
TotalUser.findOneAndUpdate({ default: "active"}, { $inc: { count: 1}},
function(err, result) {
if (err) throw err;
console.log(result);
res.send(result);
});

ADD multi:适用于所有更新

要计算活跃用户数量,请尝试以下操作:

TotalUser.aggregate(
[
{
$match: {
default:"active"
}
},
{
$count: "active_users"
}
]
)

或不聚合:

TotalUser.find({"default" : "active"}).count() 

关于node.js - Nodejs中的"BadValue Unsupported projection option"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55932284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com