@@ -19,6 +19,7 @@ async function currentlyreadingbook() {
1919 try {
2020 //1. connect to MongoDB database 'currently-reading' collection
2121 let currentlyreadingcollection = client . db ( 'books' ) . collection ( 'currently-reading' ) ;
22+
2223 //2. request read books from Goodreads XML
2324 //console.log(currentlyreadURL)
2425 request ( currentlyreadURL , ( error , req , book ) => {
@@ -28,18 +29,23 @@ async function currentlyreadingbook() {
2829 console . log ( 'Need new book recommendations' ) ;
2930 }
3031 else {
31- //4. delete existing collection in MongoDB
32- //currentlyreadingcollection.drop();
33- //console.log('Deleted currently reading book collection in MongoDB...')
34- //5. parse XML to JSON; change attrkey from '$' to something else so MongoDB accepts data
35- parseString ( book , { attrkey : '@' } , function ( err , read ) {
36- console . dir ( read . GoodreadsResponse . reviews )
37- //6. insert read books into MongoDB database
38- currentlyreadingcollection . insertMany ( read . GoodreadsResponse . reviews ) ;
39- console . log ( 'Inserted currently reading book collection into MongoDB...' ) ;
40- } ) ;
32+ //4. delete existing collection in MongoDB after every 1000 entries
33+ currentlyreadingcollection . countDocuments ( function ( error , result ) {
34+ console . log ( result )
35+ if ( result > 1000 ) {
36+ currentlyreadingcollection . drop ( )
37+ console . log ( 'Deleted currently reading book collection in MongoDB...' )
38+ }
39+ } )
40+ //5. parse XML to JSON; change attrkey from '$' to something else so MongoDB accepts data
41+ parseString ( book , { attrkey : '@' } , function ( err , read ) {
42+ //console.dir(read.GoodreadsResponse.reviews)
43+ //6. insert read books into MongoDB database
44+ currentlyreadingcollection . insertMany ( read . GoodreadsResponse . reviews ) ;
45+ console . log ( 'Inserted currently reading book collection into MongoDB...' ) ;
46+ } ) ;
4147 }
42- } ) ;
48+ } )
4349 } )
4450 } catch ( err ) {
4551 console . error ( err )
0 commit comments