forked from sheetjs/sheetjs
Tested the MongoDB scripts and fixed them
This commit is contained in:
parent
e958dbf18e
commit
59b3dae1a1
@ -12,7 +12,7 @@ let P = Promise.resolve("sheetjs");
|
||||
|
||||
/* Connect to mongodb server */
|
||||
P = P.then(async () => {
|
||||
const client = await MongoClient.connect(url);
|
||||
const client = await MongoClient.connect(url,{ useUnifiedTopology: true });
|
||||
return [client];
|
||||
});
|
||||
|
||||
@ -49,14 +49,15 @@ P = P.then(async ([client, pres, fmts]) => {
|
||||
});
|
||||
|
||||
/* Read the new file and dump all of the data */
|
||||
P = P.then(() => {
|
||||
P = P.then(([client]) => {
|
||||
const wb = XLSX.readFile('mongocrud.xlsx');
|
||||
wb.SheetNames.forEach((n,i) => {
|
||||
console.log(`Sheet #${i+1}: ${n}`);
|
||||
const ws = wb.Sheets[n];
|
||||
console.log(XLSX.utils.sheet_to_csv(ws));
|
||||
});
|
||||
return [client];
|
||||
});
|
||||
|
||||
/* Close connection */
|
||||
P.then(async ([client]) => { client.close(); });
|
||||
P.then(async ([client]) => { client.close(); });
|
@ -15,7 +15,7 @@ let P = Promise.resolve("sheetjs");
|
||||
|
||||
/* Connect to mongodb server and initialize collection */
|
||||
P = P.then(async () => {
|
||||
const client = await MongoClient.connect(url);
|
||||
const client = await MongoClient.connect(url,{ useUnifiedTopology: true });
|
||||
const db = client.db(db_name);
|
||||
try { await db.collection('wb').drop(); } catch(e) {}
|
||||
const coll = db.collection('wb');
|
||||
|
@ -3,8 +3,7 @@
|
||||
var XLSX = require("xlsx");
|
||||
|
||||
async function book_append_mongo(wb, coll, name) {
|
||||
const aoo = await coll.find({}).toArray();
|
||||
aoo.forEach((x) => delete x._id);
|
||||
const aoo = await coll.find({}, {projection:{_id:0}}).toArray();
|
||||
const ws = XLSX.utils.json_to_sheet(aoo);
|
||||
XLSX.utils.book_append_sheet(wb, ws, name);
|
||||
return ws;
|
||||
@ -12,4 +11,4 @@ async function book_append_mongo(wb, coll, name) {
|
||||
|
||||
module.exports = {
|
||||
book_append_mongo
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user