我是 MongoDb 的新手。db.connect('once', function(){}); 有什么用?
这将为每个请求创建一次连接?
当这项工作完成时,这将默认关闭连接?
mongoose.connect(uri, options);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(err, resp){
console.log(resp);
});
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。
发生错误时如何保留表单数据。通常当表单提交页面重新加载时。我正在使用把手、快速验证器、连接闪存。这是我的注册代码:
//Register User
router.post('/register', function(req, res){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
//Validation
req.checkBody('name', 'Name is required!').notEmpty();
req.checkBody('email', 'Email is required!').notEmpty();
req.checkBody('email', 'Email is not valid!').isEmail();
req.checkBody('username', 'Username is required!').notEmpty();
req.checkBody('password', 'Password is required!').notEmpty();
req.checkBody('password', 'Minimum Length of Password is 1!').isLength({min: 1});
req.checkBody('password2', 'Password do not match!').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
//want some help here by which if any error generate then all value is …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数据集:
{
"_id" : ObjectId("5c35f04c4e92b8337885d9a6"),
"activity" : {
"a1": 10,
"a2": 11,
"a3": 12,
"a4": 13,
"a5": 14,
"b1": 5,
"b2": 6
}
}
Run Code Online (Sandbox Code Playgroud)
注意:这是一个虚拟条目,实际条目约为 50 或 60 个,其中 30 个条目来自简单加法(如从 a1 到 a5),20 个条目用于比率(如 b1 和 b2)。为了询问这个概念,我最小化了数据集
现在从 a1 到 a5 我必须用乘法的一些权重值进行加法,如下所示:假设乘法值为 0.5
so it will be like this:
(a1*0.5 + a2*0.5 + a3*0.5 + a4*0.5 + a5*0.5)
Run Code Online (Sandbox Code Playgroud)
然后我必须取 b1 和 b2 的比率,然后乘以这样的权重
((b1:b2)*0.5)
Run Code Online (Sandbox Code Playgroud)
所以最终的计算将是这样的:
(a1*0.5 + a2*0.5 + a3*0.5 + a4*0.5 + a5*0.5 + (b1:b2)*0.5)
Run Code Online (Sandbox Code Playgroud)
为此我使用了 mongodb …
我有这样的数据集:
[ { city: 'a', value: 1, sector: 'Hospital' },
{ city: 'b', value: 1, sector: 'Hardware' },
{ city: 'c', value: 1, sector: 'Hardware' },
{ city: 'd', value: 1, sector: 'Networking' },
{ city: 'e', value: 1, sector: 'Hospital' },
{ city: 'f', value: 1, sector: 'Education' },
{ city: 'g', value: 1, sector: 'Transport' },
{ city: 'h', value: 1, sector: 'Food' },
{ city: 'i', value: 1, sector: 'Networking' },
{ city: 'j', value: 0.7, sector: …Run Code Online (Sandbox Code Playgroud)