1+ import { manageProducts } from './db.js'
2+ import { productCommonUrl } from './utils.js'
3+ import fs from 'fs'
4+ import { DB_URL } from "./config.js" ;
5+ import { MongoClient } from "mongodb" ;
6+ var mongo ;
7+
8+ // Check if mongodb is connected or not
9+ const isDbConnected = ( ) =>
10+ ! ! mongo && ! ! mongo . topology && mongo . topology . isConnected ( ) ;
11+
12+ // Connect to mongodb
13+ const connectDb = async ( ) => {
14+ if ( ! isDbConnected ( ) ) {
15+ try {
16+ mongo = await MongoClient . connect ( DB_URL , {
17+ useNewUrlParser : true ,
18+ useUnifiedTopology : true ,
19+ } ) ;
20+ console . log ( "Connected to Database!" ) ;
21+ } catch ( e ) {
22+ console . log ( "Failed to connected to Database!" ) ;
23+ }
24+ }
25+ } ;
26+
27+
28+ const func = async ( ) => {
29+ await connectDb ( ) ;
30+ const collection = mongo . db ( "AS_TRACKER" ) . collection ( "tasks" ) ;
31+ const result = await collection . find ( { } ) . toArray ( ) ;
32+ console . log ( result [ 0 ] . users . length ) ;
33+ let count = 0 ;
34+ result . map ( ( item ) => {
35+ count += item . users . length
36+ } ) ;
37+
38+ console . log ( count ) ;
39+ }
40+ // let data = await manageProducts({}, 'read')
41+ // data.result.map((product,i) => {
42+ // const url = productCommonUrl(product.link);
43+ // data.result[i].link = url
44+ // })
45+ // fs.writeFileSync('updated.json', JSON.stringify(data.result));
46+ // const data = fs.readFileSync('updated.json').toString();
47+ // let products = JSON.parse(data);
48+ // let arr = [];
49+ // const duplicate = products.filter(product => {
50+ // const same = products.filter(p => {
51+ // return p.link === product.link
52+ // })
53+ // const users = same.map((p) => ({ userId: p.userId, tracking_id: p.tracking_id}));
54+ // arr.push({
55+ // link: product.link,
56+ // merchant: product.merchant,
57+ // initPrice: product.initPrice,
58+ // price: product.price,
59+ // title: product.title,
60+ // users
61+
62+ // })
63+ // // same.map((p, index) => {
64+ // // delete products[index]
65+ // // })
66+ // })
67+ // fs.writeFileSync('duplicate.json', JSON.stringify(arr));
68+
69+ // (async () => await func())()
70+ const deleteData = async ( ) => {
71+ await connectDb ( ) ;
72+ const db = mongo . db ( 'TESTS' ) . collection ( 'tasks' )
73+ await db . deleteOne ( { users : null } )
74+ }
75+ ( async ( ) => await deleteData ( ) ) ( )
0 commit comments