gpt4 book ai didi

javascript - 如何将对象数组插入MongoDb集合

转载 作者:行者123 更新时间:2023-12-02 21:44:24 24 4
gpt4 key购买 nike

我正在使用 NodeJS 和 mongoose 实现用户恢复系统。我需要将已删除的用户投诉插入到已删除的用户集合中。怎么做?当我从集合中删除用户时,特定数据应该插入另一个集合名称已删除的用户。它更类似于用户恢复系统。我的模块已删除User.js

const mongoose = require('mongoose')

/**
* Data model for deletedUser.
*/

const Schema = mongoose.Schema
const deletedUserSchema = new Schema({
_id: {
type: mongoose.Schema.Types.ObjectId,
createIndex: true,
required: true,
auto: true
},
email: {
type: String,
required: true,
unique: true,
match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
},
password: String,
username: String,
firstname: String,
lastname: String,
telephone: String,

//address
line1: String,
line2: String,
line3: String,

nic: String,

broadcasts: [],
selling: [],
complains: [],
bookmarks: [],
sellerReply: [],
finalized: [],

overallrate: Number,
ratings: [],

profileImage: String,
replying:[],

purchaseHistory: [],
salesHistory: [],
latestLog: Date,

longitude: Number,
latitude: Number,
// expire_at: {type: Date, default: Date.now, expires: 3600*24*100}

})
module.exports = mongoose.model('deletedUser', deletedUserSchema, 'deletedUser')

这是我迄今为止的尝试

// save deleted user data
router.post('/delete/:Id', async (req, res) => {
const selectedUser = await User.findById(req.params.Id);
// const deleteduser = new deletedUser({})
// res.json(selectedUser._id);
let testdata = [];
// selectedUser.complains=this.testdata;
console.log("When deleted"+selectedUser.complains)
const deleteduser = new deletedUser({
_id: selectedUser._id,
email: selectedUser.email,
password: selectedUser.password,
username: selectedUser.username,
firstname: selectedUser.firstname,
lastname: selectedUser.lastname,
telephone: selectedUser.telephone,
line1: selectedUser.line1,
line2: selectedUser.line2,
line3: selectedUser.line3,
nic: selectedUser.nic,
broadcasts: selectedUser.broadcasts,
selling: selectedUser.selling,
complains: testdata.push(selectedUser.complains),
bookmarks: selectedUser.bookmarks,
sellerReply: selectedUser.sellerReply,
finalized: selectedUser.finalized,
overallrate: selectedUser.overallrate,
ratings: selectedUser.ratings,
profileImage: selectedUser.profileImage,
replying: selectedUser.replying,
purchaseHistory: selectedUser.purchaseHistory,
salesHistory: selectedUser.salesHistory,
latestLog: selectedUser.latestLog,
longitude: selectedUser.longitude,
latitude: selectedUser.latitude

});
deleteduser.save().then(data => {
res.json(data);
}).catch(err => {
res.json({ message: err })
});

});

最佳答案

您当前的方法对我来说似乎还不错,但是,您可以减少 document.toObject 涉及的代码。方法:

router.post('/delete/:Id', async (req, res) => {
const selectedUser = await User.findById(req.params.Id);

console.log("When deleted"+selectedUser.complains)

// use toObject to convert the user model into a normal javascript object
const deleteduser = new deletedUser(selectedUser.toObject());
deleteduser.save().then(data => {
res.json(data);
}).catch(err => {
res.json({ message: err })
});

});

关于javascript - 如何将对象数组插入MongoDb集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60295237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com