本文整理汇总了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) => {
//.........这里部分代码省略.........