我在使用nodejs的mongojs驱动程序在mongodb中插入实际的datetime对象时遇到了问题.有什么帮助吗?

var currentdate = new Date(); 
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1)  + "/" 
+ currentdate.getFullYear() + " @ "  
+ currentdate.getHours() + ":"  
+ currentdate.getMinutes() + ":" 
+ currentdate.getSeconds();

db.test.update({
    conversation: conv
},{ 
    $push:{ messages: {
        message: message,
        pseudo: name,
        current_date: datetime
    }}
},{upsert: true});

推荐答案

您不需要手动创建所有这些日期.

db.test.update({
    conversation: conv
}, { 
    $push:{ messages: {
        message: message,
        pseudo: name,
        current_date: new Date()
    } }
}, {
    upsert: true
});

我会做的.

还请记住,在Mongo 2.6中,在许多其他功能中,您可以使用$currentDate,这可能很方便.

Mongodb相关问答推荐

通过mongoDB中的查找从管道中删除被阻止的用户

为什么数组长度0被认为是Mongoose中排序中最大的数字

无法通过管理邮箱/密码身份验证注册/登录新用户帐户(类型错误:加载失败)

如何获取mongodb ObjectId的值?

MongoDB:从开始日期和结束日期数组中匹配特定日期的聚合查询

MongoDB - 在 $lookup 管道中匹配键匹配不起作用

$eq 的目的是什么

MongoDb c# driver LINQ vs Native 查询

如何在 Mongo 聚合中合并文档中的数组字段

声明多个模式后无法从数据库中获取数据(mongoose + express + mongodb

为什么 local.oplog.rs 上每隔几分钟的活动就会锁定 mongo 客户端

获取收集字节使用情况统计信息的pymongo方法?

删除嵌套文档数组中的嵌入文档

Node.js 和 Passport 对象没有方法 validPassword

如何使用 MongoDB 以编程方式预拆分基于 GUID 的分片键

具有 AWS 云形成和自动zoom 的 Mongodb 集群

Mongodb $lookup 使用 _id 无效果

MongoTemplate upsert - 从 pojo 进行更新的简单方法(哪个用户已编辑)?

AsQueryable 方法是否在新的 Mongodb C# 驱动程序 2.0rc 中离开?

如何在 Windows 上停止 mongodb 服务器?