如何解决如何在 mongodb 指南针中使用数组保存 csv 文件?
问题图片:
我安装 csv 文件没有任何问题,但它将数组视为字符串。类别必须是数组。
示例代码
{ "_id":{"$oid":"609196655b92b7164b2cb016"},"body":"test","category":"["Aksiyon Oyunları","Beceri Oyunları"]","date":"1617272388425","gamelink":"ssdsd","imglink":"deneme görseli","status":"published","title":"BUNU YAPIN TREN!","slug":"bunu-yapin-tren!" }
解决方法
const postSchema = new Schema({
title: {
type: String,required: true
},category: {
type: [String],required: true
}
});
module.exports = new model('posts',postSchema);
架构和此代码与节点 js
mongoose
.connect(MONGO_URL)
.then(() => console.log('DB Connected'))
.catch(err => console.log("Fail to connect mongodb ",err));
var ops = [];
post_schema.find({ "category": { "$type": 2} },function(err,arr) {
arr.forEach(doc => {
let str = doc.category+``
str = str.split(',');
const arr = str.map(e=>e.replace(/"|\[|\]|\\/gm,'').toString())
ops.push({
"updateOne": {
"filter": { "_id": doc._id },"update": { "$set": { "category": arr } }
}
});
if ( ops.length > 0 ) {
console.log("BULKWRITE")
post_schema.bulkWrite(ops);
ops = [];
}
});
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。