node.js - 如何在 MongoDB 中手动放置 createdAt (timestamp)
问题描述
如何在 MongoDB 中添加手动 createdAt (timestamp) 字段?
db.getCollection("properties").find({})
这将返回所有属性,这是一个示例:
{
"_id" : ObjectId("5e6b3269617d327b82a71723"),
"title" : "Nice and cozy apartment",
"category" : "rent",
"town" : "London",
"price" : NumberInt(3100),
"square" : NumberInt(249),
}
我想要实现的是一个带有时间戳的字段或有关创建特定属性的日期的信息。有人可以帮忙吗?
解决方案
只需在猫鼬模式中放一个键
createdAt:{type: Date, default: Date.now()};
//will add automatically putt entry in db whenever document created.
并使用聚合而不是查找(因为您在旧文档中没有密钥 createdAt)作为,
db.orders.aggregate([
{
$addFields: {
"createdAt": { $ifNull: ["$createdAt", Date(manually whenever created)] },
}
}
])
推荐阅读
- python - 需要帮助或理解
- windows - 从 Android Studio 运行我的应用程序时,我可以强制模拟器到顶部吗?
- css - iPhone XR / XS / XS Max CSS 媒体查询
- scala - 现在 22 不再是限制,Scala 案例类中允许的实际最大字段数是多少?
- linux - 查找:在 Linux 中缺少“-exec”的参数
- javascript - 文本区域的动态高度
- c# - roslyn 未使用 PackageReference 和 Microsoft.CodeDom.Providers 2.0.0 复制到 AspNet Mvc 5 项目的 bin
- postman - 读取作为文本发布到金字塔网络应用程序的文件
- excel - VBA按索引打印到PDF多张纸?
- angular - Pro FontAwesome 图标“找不到图标”