SQL:如何生成下个日期的日期(SQL: How to produce next date given month and day)
在我的表中,我有一个月(tinyint)和一天(tinyint)的领域。 我想有一个函数,它需要这个月份和日期,并为这个月份和下一天的下一个日期(包括年份)生成一个日期时间。
所以如果我有月份= 9,日= 7,它会产生9/7/2009。
如果我有第一个月,第一天它会产生2010年1月1日。In my table I have a Month(tinyint) and a Day(tinyint) field. I would like to have a function that takes this month and day and produces a datetime for the next date(including year) given this month and day.
So if I had Month = 9, Day = 7 it would produce 9/7/2009.
If I had Month 1, Day 1 it would produce 1/1/2010.
原文:https://stackoverflow.com/questions/1034901
最满意答案
在javascript中,你不能使用变量作为对象文字中的属性名称,这就是你正在做的。
尝试一下:
var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o);
将打印
{ a: 'somePropertyValue' }
不是{someProperty:'somePropertyValue}
。如果javascript允许在对象文字表示法中引用属性名称中的变量,则它必须除去未加引号的名称,因为这会导致不明确的名称。
a
应该用作属性的值还是应该是变量a
的值?尝试使用事先创建的对象crate创建更新对象文本,而不使用对象文字符号,因此您的代码如下所示:
router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); //We create the $set property/value pair using property assignment, not the object literal var updateVal = {}; updateVal[type] = newValue; collection.update( {"_id" : id}, {$set: updateVal} //use it here , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } } ); });
更好的是,预先构建整个
$set
操作。In javascript you cannot use variables as property names in object literals, and that's what you're trying to do.
Try it:
var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o);
will print
{ a: 'somePropertyValue' }
not{someProperty:'somePropertyValue}
.If javascript permitted referencing variables in property names in object literal notation it would have to get rid of unquoted names as those would create ambiguity. Should
a
be used as the value of the property or should it be the value of the variablea
?Try creating the update object literal with an object crated beforehand without the usage of object literal notation, so your code looks something like this:
router.get('/vote/:Id/:Value/:Type', function(req, res) { var db = req.db; var id = req.params.Id; var type = req.params.Type; var value = parseInt(req.params.Value); var newValue = value + 1; var collection = db.get('games'); //We create the $set property/value pair using property assignment, not the object literal var updateVal = {}; updateVal[type] = newValue; collection.update( {"_id" : id}, {$set: updateVal} //use it here , function (err, doc) { if (err) { res.send("There was a problem"); } else { res.location("../../../admin"); res.redirect("../../../admin"); } } ); });
Even better, construct the whole
$set
operation beforehand.
相关问答
更多-
在javascript中,你不能使用变量作为对象文字中的属性名称,这就是你正在做的。 尝试一下: var a = 'someProperty'; var o = {a: 'somePropertyValue'}; console.log(o); 将打印{ a: 'somePropertyValue' }不是{someProperty:'somePropertyValue} 。 如果javascript允许在对象文字表示法中引用属性名称中的变量,则它必须除去未加引号的名称,因为这会导致不明确的名称。 a ...
-
您可以使用$push或$addToSet数组运算符通过单个findAnyModify调用直接将新影片添加到watched_movies数组: var movie = { "name" : "InterStellar", "Year" : "2014" }; collection.findAndModify( {'_id':new BSON.ObjectID(id)}, [['_id','asc']], {$addToSet: {"watched_movies": movie}}, ...
-
这样的事情对你有用。 查找并替换所选房间的患者。 department.findOneAndUpdate( { "rooms.patient.patientnr": parseInt(req.params.id) }, { "$set": {"rooms.$.patient": patient}}, {new : true} .... ) Something like this should work for you. Finds and replace the patie ...
-
我尝试了一些不同的东西,似乎猫鼬不喜欢$。 当我在做“mPlayers.1”时,它就像一个魅力:) I tried some different things and it seems like mongoose doesn't like the $. When I'm doing "mPlayers.1" it works like a charm :)
-
在您的第一个代码示例中,您说: 为此我得到的文字没有定义 我猜你的意思是“ 测试未定义?” 您的脚本只需要mongodb库,我不相信test是核心nodejs函数,因此可以解释错误。 要引用db.collection()的驱动程序文档,请使用assert库,但也要正确导入(如第二个示例中所做的那样)。 在回调db.open() ,不检查是否发生了错误。 这可能会说明为什么db在该函数中为null。 关于与MongoDB的SQL注入等效的问题,主要关注的领域是你可以将不受信任的输入传递给已评估的JavaScr ...
-
由于您使用的是mongo,因此可以将所有这些键放在索引中。 如果您的文档如下所示: doc = { 'users': 50, 'page': 10, 'articles': 20 } 干得好: db.foo.ensureIndex( { 'users': 1, 'page': 1, 'articles': 1 }) 然后确保只返回这些键。 如果它们在索引中,Mongo将直接从内存而不是从磁盘读取这些值。 如果要通过类似_id的方式查询,只需将_id添加为索 ...
-
这实际上只是一个执行顺序问题。 MongoClient.connect(...)调用是异步的 。 在建立连接之前,不会调用您传递的回调函数。 但是,在调用回调之前 ,会立即调用最后的console.log(global.db) 。 实际上你不需要在这里明确地创建db global。 在模块的外部范围中定义的变量位于模块的全局范围内。 将值附加到global使其可以跨模块使用,但是在模块之间公开值的更好方法是将它们附加到exports ,以便可以在需要时显式导入它们。 如果您只在此模块中使用db ,那么这不是 ...
-
节点不会增加4个小时。 两者都显示完全相同的瞬间。 2014-10-01 10:28:04.329-04:00 与...完全相同 2014-10-01T14:28:04.329Z 只有一个在EDT时区中,它与UTC的-04:00偏移(所以它早于4小时),另一个是UTC。 您可能在EDT中配置了服务器,并且您的客户端设置为UTC或反之亦然。 除非你需要完全相同的字符串,否则我不担心。 或者,更好的是,将客户端和服务器计算机设置为相同的时区, 最好是UTC 。 Node isn't adding 4 hou ...
-
您正尝试在浏览器上运行服务器JavaScript代码。 JavaScript不再是客户端脚本语言。 nodeJS使用JavaScript来运行服务器框架,并且日益流行。 我现在不知道如何使用驱动程序..我试过这个: 不熟悉nodeJS这个错误。 您必须了解nodeJS与提供HTML页面的任何其他服务器一样。 在服务器上,您有服务器端脚本,执行服务器以及服务器提供的客户端内容。 只有JavaScript在nodeJS中的服务器和客户端上nodeJS 。 在学习使用mongodb软件包之前,您应该学习如何使用n ...
-
首次创建collection时, Indexes (如在任何数据库中)只需创建一次。 索引创建是一个代价高昂的操作和阻止操作。 从mongoDB 3.0 , createIndex()和ensureIndex()方法之间没有区别,并且它们中的任何一个都应该用于在集合上创建索引(仅在服务器端创建索引一次)在需要的时候。 要索引title和ingredients字段,您需要在集合上创建index : db.collection.createIndex({"ingredients":"text","title": ...