Node.js 无法更新mongoose中的createdAt属性?
各位专家您好, 也许这个问题看起来是重复的,但我的商业案例有点不同,请耐心听我说 我正在使用其他开发人员定义的旧模式,其中缺少Node.js 无法更新mongoose中的createdAt属性?,node.js,mongodb,express,mongoose,data-migration,Node.js,Mongodb,Express,Mongoose,Data Migration,各位专家您好, 也许这个问题看起来是重复的,但我的商业案例有点不同,请耐心听我说 我正在使用其他开发人员定义的旧模式,其中缺少createdAt和updatedAt属性。因此,我通过添加{timestamps:true}更新了模式。每当我创建新文档时,就没有问题了。它按预期工作,但存在现有/旧文档,因此我试图通过编写单独的脚本并从\u id属性(如\u id.getTimestamp()读取创建时间)来添加createdAt属性。我可以从_id提取创建的时间,但无法在现有文档中添加created
createdAt
和updatedAt
属性。因此,我通过添加{timestamps:true}
更新了模式。每当我创建新文档时,就没有问题了。它按预期工作,但存在现有/旧文档,因此我试图通过编写单独的脚本并从\u id
属性(如\u id.getTimestamp()
读取创建时间)来添加createdAt
属性。我可以从_id提取创建的时间,但无法在现有文档中添加createdAt
属性。旧文档/数据非常重要,但业务部门希望将createdAt
property添加到旧文档中
我使用的是猫鼬NPM库,版本为:“猫鼬”:“^5.9.16”,
模式示例如下:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const profileSchema = new Schema(
{
name: {
type: String,
required: true
},
profile_number: {
type: Number
},
view: {
type: String
},
},
{ timestamps: true }
);
module.exports = mongoose.model("profile", profileSchema, "profile");
用于添加createdAt属性并从提取时间戳的脚本\u id
const mongoose = require("mongoose");
const Profile= require("../../models/Profile/ProfileModel");
/****
* * Fetch all Portfolios List
* */
exports.getAllProfileList = async (req, res, next) => {
try {
const profileResult = await Profile.find({},{_id:1}).exec(); //return all ids
const timeStampExtract = await profileResult.map(item => {
console.log("Item is:", item._id.getTimestamp());
// trying to update createdAt time by extacting item._id.getTimestamp()
Profile.findOneAndUpdate(
{ _id: item._id },
{ $set: { createdAt: item._id.getTimestamp() } },
{ upsert: true }
)
.then(res => console.log("Response:"))
.catch(error => console.log("Error is:", error.message));
return item._id.getTimestamp();
});
if (!profileResult ) {
return res
.status(400)
.json({ message: "Unable to find profile list", statusCode: 400 });
}
res
.status(200)
.json({ totalCount: profileResult.length, data: timeStampExtract });
} catch (error) {
logger.error(error.message);
return res.status(500).json({ message: error.message, statusCode: 500 });
}
};
默认情况下,createdAt是不可变的吗
有人能帮忙吗?这将是很大的帮助
感谢所有专家,因为有些文档是在
时间戳
选项设置为false(这是默认值)时创建的,所以mongoose不会知道这些时间戳。因此,item.\u id.getTimestamp()
将返回undefined
您可以做的是重新创建createdAt
不存在的条目。如果启用该选项,Mongoose将自动生成时间戳并将其设置为当前时间戳:
const profilesWithoutCreated = await Profile.find({createdAt: {$exists: false}}).exec();
const timeStampExtract = [];
let newProfile;
for (const profile of profiles) {
newProfile = new Profile(profile);
newProfile.createdAt = profile._id.getTimestamp();
const savedProfile = await newProfile.save();
timeStampExtract.push(savedProfile._id.getTimestamp());
}
它不起作用,我试过了。createdAt没有添加当我们在模式中使用{timestamps:true}时,它将createdAt和updatedAt属性添加到文档中。我猜,createdAt是不可变的,因为它不允许更新值。如果我们将添加除createdAt name之外的其他键,如created_,则它正在工作,它正在保存模型。谢谢你的帮助。我对答案做了些小改动。请检查一下,你能帮我一下吗:当我们在模式中使用{timestamps:true}时,它正在向文档中添加createdAt和updatedAt属性。我猜,createdAt是不可变的,因为它不允许更新值。如果我们在createdAt name之外添加其他键,比如created_,那么它正在工作