如果我们没有在insert语句中提供值,我想在mongodb
文档中创建当前时间作为默认值。
在mysql中,通过使用以下语句可以提供相应的证明。如果我们没有插入值,则默认时间值将分配给该行。
create table test(name varchar(100), age varchar(10),created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
在mongodb中是否有类似的方法?我正在使用CSV文件中的mongoimport
将数据导入mongodb。因此,对于每个CSV行,我想要一个名为“createdAt”的额外字段,其中包含文档导入时间的值。
mongoimport -d aptigo -c employee --type csv --file /home/weavers/files/csv/Employees.csv --headerline
答案 0 :(得分:0)
Mongo中没有规定插入默认时间戳,而不是从_id值插入时间戳。
ObjectId("507c7f79bcf86cd7994f6c0e").getTimestamp()
希望这会对你有所帮助。
答案 1 :(得分:0)
您正在使用mongoose,因为在创建架构时,您可以将默认日期定义为
new Schema({ createdAt: { type: Date, default: Date.now } })