我在 MongoDB 上有一个名为“权限”的集合。我想实现一个这样的简单更新:
let schema = new Schema({
title: String
});
let Permissions = mongoose.model("Permission", schema);
let permission = new Permissions();
let query = {};
let newValues = {
$set: {
title: "Yes"
}
};
permission.updateOne(query, newValues, (err, docs) => {
console.log(err); // null
console.log(docs); // { ok: 0, n: 0, nModified: 0 }
if (err) return cast.error(err);
return cast.ok();
});
Run Code Online (Sandbox Code Playgroud)
不过,我收到{ ok: 0, n: 0, nModified: 0 }在控制台日志docs
和null在控制台日志err。
我究竟做错了什么?
我有一个大数组,我想进行自动完成搜索,但是我只想显示10个结果,所以在找到10个结果时停止对数组进行迭代。我做了这个:
let items = array.filter(r => r.indexOf(term)!=-1);
console.log(items.length) // lots of items, need to be limited to 10
Run Code Online (Sandbox Code Playgroud)
它有效,但我不知道如何在array.filter达到所需限制时停止。
我在 MongoDB 中有一个用户集合。我已将“状态”键定义为索引,如下所示:
db.users.createIndex("status": 1)
Run Code Online (Sandbox Code Playgroud)
我正在尝试查找状态设置为“活动”的所有用户。
我的数据库共有 24 个用户。其中 17 个处于活跃状态。
执行下一个命令时:
db.users.find("status": "Active").explain("executionStats")
Run Code Online (Sandbox Code Playgroud)
我收到下一个:
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 17,
"executionTimeMillis" : 2,
"totalKeysExamined" : 17,
"totalDocsExamined" : 17,
...
Run Code Online (Sandbox Code Playgroud)
TotalDocsExamined很清楚 - Mongo 总共扫描了 17 个文档并跳过其余 7个文档(请记住,我们总共收集了 24 个用户)。这太棒了,而且非常高效——因为我要求查找并拥有每个文档的数据。
但是当我执行相同的查询时,但现在使用该.count()方法 - 我不希望 Mongo 扫描 17 条记录,我希望他扫描 0 条记录并立即给我结果总数 - 而无需迭代整个数据库 -因为当有数十亿条记录时,此任务可能会关闭整个服务器。
所以我正在这样做:
db.users.explain("executionStats").find("status": "Active").count()
Run Code Online (Sandbox Code Playgroud)
我收到这个:
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 0,
"executionTimeMillis" : 0,
"totalKeysExamined" : 18,
"totalDocsExamined" : …Run Code Online (Sandbox Code Playgroud) 使用 Joi 模式验证,是否可以针对 MongoDB ObjectID 进行验证?
像这样的事情可能很棒:
_id: Joi.ObjectId().required().error(errorParser),
Run Code Online (Sandbox Code Playgroud) 如果我这样做:
client.get("foo", (err, res) => {
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
Redis 服务器中存储了数十亿个密钥,它是否会像只存储几个密钥一样快速返回数据?
或者我应该使用索引(如果 Redis 中有索引),就像在 MongoDB 中查询数据库一样?
我正在使用body-parser中间件来处理 REST API 中的 JSON 请求正文。
我试图“破解”并测试系统如何处理这样的输入:
// Note the "form": "a" does not include the required ","
{
"from": "a"
"destination": "Netanya",
"date": {
"start": "15-07-2018"
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道在哪里可以捕获这种错误输入语法的错误。
我试图删除正文解析器并且没有抛出错误,但是当然,我不能在req.body.
UnhandledPromiseRejectionWarning 在异步等待中
我有以下代码:
function foo() {
return new Promise((resolve, reject) => {
db.foo.findOne({}, (err, docs) => {
if (err || !docs) return reject();
return resolve();
});
});
}
async function foobar() {
await foo() ? console.log("Have foo") : console.log("Not have foo");
}
foobar();
Run Code Online (Sandbox Code Playgroud)
结果如下:
(节点:14843)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):false
(节点:14843)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零的退出代码终止Node.js进程。
注意:我知道我可以解决以下问题:
foo().then(() => {}).catch(() => {});
Run Code Online (Sandbox Code Playgroud)
但是,然后我们“回到”回调异步样式。
我们如何解决这个问题?
我想检测用户的翻译文件并基于它来获得所需的方向。例如,HE 将获得 RTL,EN 将获得 LTR。
但是我没有找到在<html dir="">标签中实现数据的方法
这是 index.html 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ionic App</title>
<meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#4e8ef7">
<!-- add to homescreen for ios -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<!-- cordova.js required for cordova apps (remove if not needed) -->
<script src="cordova.js"></script>
<!-- un-comment this code to enable service worker
<script> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Express 获取客户端的 IP:
\nconst ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;\nRun Code Online (Sandbox Code Playgroud)\n当鼠标悬停在 VS Code 中的 req.connection 上时,我收到警告:
\n(property) IncomingMessage.connection: Socket\n@deprecated \xe2\x80\x94 since v13.0.0 - Use socket instead.\n\n'connection' is deprecatedts(6385)\nRun Code Online (Sandbox Code Playgroud)\n如何实现“套接字替代”/查找客户端 IP 的正确方法是什么?
\n我在聚合管道的第一阶段使用以下方法得到了这组结果$match:
[
{ a: 1, b: 2 },
{ a: 3, b: 4 }
]
Run Code Online (Sandbox Code Playgroud)
现在我想对所有 A 和 B 求和,并且仍然保留它们,因此我将得到如下结果:
{
total_sum: 10,
items: [...] // first and second objects ofcourse
}
Run Code Online (Sandbox Code Playgroud)
我尝试过$group,$push但是,push 只从对象中推送特定字段,我需要命名 A 和 B,而不是解析所有它们。
我该怎么做?
mongodb ×4
node.js ×4
javascript ×2
nosql ×2
aggregation ×1
angular ×1
body-parser ×1
count ×1
crud ×1
database ×1
express ×1
joi ×1
mongoose ×1
objectid ×1
performance ×1
redis ×1