如何导入csv文件到MongoDb(使用NodeJS/Express/EJS)



我在一个项目中有可能一次添加几个项目。我希望能够使用csv文件添加多个项目。

有更多的上下文(模式):

{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"nameCourse": "Name",
"description": "lorem ipsum",
"user": [{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "John Doe",
"gender": "M",
"city": "New York",
"email": "johndoe@email.com"
}, 
{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "Jane Doe",
"gender": "F",
"city": "Tokyo",
"email": "janedoe@email.com"
},
{
"_id": {"$oid": "xxxxxxxxxxxxxxxx"},
"name": "Jake Doe",
"gender": "M",
"city": "Paris",
"email": "jakedoe@email.com"
}],
}

(我使用NodeJs/MongoDb(与Mongoose)/Express/EJS)

它有一个带有输入文件和按钮的表单。

<form method="POST" id="add_multiple_user">
<input type="file" name="user">
<button type="submit">Send</button>
</form>
<script>
$("#add_multiple_user").submit(function (event) {
event.preventDefault();
let idcourse = document.getElementById("id_course");
let unindexed_array = $(this).serializeArray();
let data = {};
$.map(unindexed_array, function (n, i) {
data[n["name"]] = n["value"];
});
let addAdresseMultiple = {
url: `/api/course/add-multiple-user/${idcourse.innerText}`,
method: "PUT",
data: data,
};
$.ajax(addAdresseMultiple).done(function (response) {
location.reload();
});
});</script>

这里是我的控制器:

exports.addMultipleUser = async (req, res) => {
const { name, gender, city, email } = req.body;
try {
await UserModel.findOneAndUpdate(
{ _id: req.params.id },
{
$addToSet: {
user: {
name: name,
gender: gender,
city: city,
email: email,
},
},
},
(err, docs) => {
if (!err) return res.send(docs);
if (err) return res.status(400).send({ message: err });
}
);
} catch (err) {
return res.status(400).json({ message: err });
}
};

我看到有一些包(像快速csv),但我不明白它怎么能帮助我。

我不打算为你发布任何答案,但这里有一个你可能需要突破的快速总结。

  1. 理解乘
  2. 将CSV转换为JSON
  3. 创建多个项目

感谢@Koodies指引我走上正确的道路。

答案如下:

控制器:

exports.addMultipleUser = async (req, res) => {
try {
await csv({delimiter:";"})
.fromFile(req.file.path)
.then((jsonObj)=>{
console.log(jsonObj);
UserModel.findOneAndUpdate(
{ _id: req.params.id },
{ $addToSet: { sites: jsonObj } },
(err, docs) => {
if (!err) return res.send(docs);
if (err) return res.status(500).send({ message: err });
}
);
fs.unlink(req.file.path, (err) => {if(err){console.log(err); return;}})
});

} catch (err) {
return res.status(500).json({ message: err });
}
};

my form &jquery:

<form method="POST" id="add_multiple_user" >
<input type="file" name="user" id="myFileUser">
<button type="submit">Send</button>
</form>
<script>$("#myFileUser").change(function (e) {
let file = e.target.files[0];
let formData = new FormData();
formData.append("myFileUser", file);
$("#add_multiple_user").submit(function (event) {
event.preventDefault();
let idcourse = document.getElementById("id_course");
$.ajax({
url: `/api/course/add-multiple-user/${idcourse.innerText}`,
type: "PUT",
data: formData,
processData: false,
contentType: false,
success: function (res) {
console.log(res);
},
crossDomain: true,
}).done(function(res) {location.reload()});
});

});</script>

相关内容

  • 没有找到相关文章

最新更新