當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Collection.insertMany方法代碼示例

本文整理匯總了TypeScript中mongodb.Collection.insertMany方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Collection.insertMany方法的具體用法?TypeScript Collection.insertMany怎麽用?TypeScript Collection.insertMany使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mongodb.Collection的用法示例。


在下文中一共展示了Collection.insertMany方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: genId

    col.drop().catch(err => err).then(() => {
        console.log('Start DB Creation');
        console.time('Create DB');
        let completedCreation:Promise<any>;
        if (mode == DBMode.RacerTable) {
            
            let toAdd:Array<IndependentRacer> = new Array<IndependentRacer>();
            for (let team = 0; team < teams; ++team) {
                // generate team id
                let teamId = genId(team);
                for (let racer = 0; racer < racersATeam; ++racer) {
                    // generate racer
                    toAdd.push(generateIndependentRacer(teamId, genId(team * racersATeam + racer)));
                }
            }
            console.log('Racer Obj Created, Inserting');
            completedCreation = Promise.all([col.insertMany(toAdd), col.createIndex('id')]);
            
        } else {
            
            let toAdd:Array<Team> = new Array<Team>();
            for (let team = 0; team < teams; ++team) {
                // create team, generate id
                let newTeam:Team = new Team(genId(team), []);
                toAdd.push(newTeam);
                for (let racer = 0; racer < racersATeam; ++racer) {
                    // add random racers to team
                    newTeam.racers.push(generateRacer(genId(team * racersATeam + racer)));
                }
            }
            console.log('Teams Obj Created, Inserting');
            completedCreation = Promise.all([col.insertMany(toAdd), col.createIndex('racers.id'), col.createIndex('id')]);
        }
        // once the collection and indices have been created, run our tests
        completedCreation.then(() => {
            console.timeEnd('Create DB');

            // create the threads that will access the database
            let threads:Array<ChildProcess> = new Array<ChildProcess>();
            console.log("Creating Threads");
            for (let i = 0; i < threadCount; ++i) {
                let new_child:ChildProcess;
                if (mode == DBMode.RacerTable) {
                    new_child = fork('./stress-test-racer');
                } else if (mode == DBMode.TeamTable) {
                    new_child = fork('./stress-test-team');
                }
                new_child.send({
                    type:             'generate',
                    teams:            teams,
                    racersATeam:      racersATeam,
                    updateQueryRatio: updateQueryRatio,
                    thisThreadNum:    i,
                    totalThreads:     threadCount,
                    accessCount:      accessCount
                });
                threads.push(new_child);
            }
            // run the three tests once after another
            async.series([
                (callback:(err:Error)=>void) => {
                    // Wait for all threads to be ready
                    Promise.all(threads.map(thread=>new Promise((resolve, reject)=> {
                        thread.once('message', (msg:string) => msg === 'ready' ? resolve() : reject())
                    }))).then(() => {
                        console.log('All Threads Ready');
                        callback(null);
                    }).catch(() => callback(new Error('Thread Error')));
                },
                (callback:(err:Error)=>void) => {
                    console.time("Update from all Threads");
                    Promise.all(threads.map(thread=>new Promise((resolve, reject)=> {
                        thread.send('update');
                        thread.once('message', (msg:string) => msg === 'update' ? resolve() : reject());
                    }))).then(() => {
                        console.timeEnd("Update from all Threads");
                        callback(null);
                    }).catch(() => callback(new Error('Thread Error')));
                },
                (callback:(err:Error)=>void) => {
                    console.time("Query from all Threads");
                    Promise.all(threads.map(thread=>new Promise((resolve, reject)=> {
                        thread.send('query');
                        thread.once('message', (msg:string) => msg === 'query' ? resolve() : reject());
                    }))).then(()=> {
                        console.timeEnd("Query from all Threads");
                        callback(null);
                    }).catch(() => callback(new Error('Thread Error')));
                },
                (callback:(err:Error)=>void) => {
                    console.time("Update and Query from all Threads");
                    Promise.all(threads.map(thread=>new Promise((resolve, reject)=> {
                        thread.send('both');
                        thread.once('message', (msg:string) => msg === 'both' ? resolve() : reject());
                    }))).then(()=> {
                        console.timeEnd("Update and Query from all Threads");
                        callback(null);
                    }).catch(() => callback(new Error('Thread Error')));
                },
                (callback:(err:Error)=>void) => {
//.........這裏部分代碼省略.........
開發者ID:Tiedye,項目名稱:RazzieTest,代碼行數:101,代碼來源:index.ts


注:本文中的mongodb.Collection.insertMany方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。