【发布时间】:2023-03-10 09:43:01
【问题描述】:
尝试在 mongodb 中保存日期格式,如 dd/mm/yyyy,但保存为 2020-06-01T18:30:00.000Z。我想在 mongodb 中保存为 2020-06-01。怎么做? 这是我的代码:
data.model.js:
const mongoose = require('mongoose');
var userSchemaData = new mongoose.Schema({
p_id: {
type: String
},
product_today: {
type: Date
},
product_afterfive: {
type: Date
}
}, {
versionKey: false,
collection: 'data'
});
module.exports = mongoose.model('Data', userSchemaData);
data.controller.js:
var moment = require('moment');
var todayDate = new Date();
var afterfiveDays = new Date(new Date().getTime() + (5 * 24 * 60 * 60 * 1000));
module.exports.insertData = (req, res, next) => {
let collectionName = req.query.collection;
var Product= mongoose.model(collectionName);
var todayDateFormat = moment(todayDate, 'DD-MM-YYYY').format('MM-DD-YYYY');
var afterfiveDaysFormat = moment(afterfiveDays, 'DD-MM-YYYY').format('MM-DD-YYYY');
var product = new Product({
p_id: "CS1",
product_today:todayDateFormat,
product_afterfive: afterfiveDaysFormat
});
product.save(function(error, document) {
if (error) { console.log(error); } else {
console.log("Successful inserted");
res.json({ success: true, msg: 'User inserted.', cname: collectionName });
}
});
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query