本文整理匯總了TypeScript中mongodb.Db類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Db類的具體用法?TypeScript Db怎麽用?TypeScript Db使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Db類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: save
async save(todo:TodoModel):Promise<TodoModel> {
let db: Db = await this.mongoDataSource.getConnection();
let insert = { name : todo.name, description : todo.description, completed : todo.completed };
let persistedTodo = await db.collection('todoCollection').insertOne(insert);
let persistedTodoId: string = persistedTodo.insertedId.toString();
return await this.get(persistedTodoId);
}
示例2: update
async update(todo:TodoModel):Promise<TodoModel> {
let db: Db = await this.mongoDataSource.getConnection();
let query = { "_id" : new ObjectID(todo.id) };
let update = { $set: { name : todo.name, description : todo.description, completed : todo.completed } };
let persistedTodo = await db.collection('todoCollection').findOneAndUpdate(query, update);
return this.get(persistedTodo.value._id.toString());
}
示例3: analysisAllUserAsyncAwait
//async/await 類 async.whilst 方式遍曆
export default async function analysisAllUserAsyncAwait() {
let endtime = new Date().getTime() / 1000;
let arg = await GetDB("bi");
let db: Db = arg.db as Db;
let LastAnalysisTimeCol: Collection = db.collection('lastanalysistime');
let AccountCol: Collection = db.db("user").collection('account');
//上次分析時間
let begintime: any = await new Promise((resolve, reject) => {
LastAnalysisTimeCol.findOne({ jobtype: "user" }, (err, lastinfo) => {
begintime = (lastinfo) ? lastinfo.time : 0; //有數據,從這數據開始,到現在。
resolve(begintime);
})
});
//要分析的telphone
let telphones: any = await new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
AccountCol.distinct('telphone', (err, telphones) => {
resolve(telphones);
})
})
});
//遍曆分別更新
(function () {
let num = -1;
return Async.whilst(
function () { return (num++ < telphones.length - 1); },
function () {
return new Promise((resolve, reject) => {
accessWebAPI('www.google.com', '/bytelphone/' + telphones[num], 'GET', null, function (err, apidata) {
if (!err && apidata.item) {
resolve(apidata.item);
} else {
resolve(null);
}
})
}).then((item: any) => {
let lastVisitAt = (new Date(item.lastVisitAt)).getTime() / 1000;
return new Promise((resolve, reject) => {
AccountCol.update({ _id: item.id }, { $set: { field: item.field } }, (err, count) => {
resolve(count);
})
})
})
}
)
})()
//更新時間
await new Promise((resolve, reject) => {
LastAnalysisTimeCol.update({ jobtype: "user" }, { $set: { time: endtime } }, { upsert: true }, (err, doc) => {
resolve();
})
});
//清理連接
db.close();
}
示例4: ObjectID
this._client.connect(DaoConstants.CONNECTION_URL, (err: any, db: Db) => {
if(isAdmin) {
db.collection('organisations').updateOne({'_id': new ObjectID(organisationId)}, {$push: {'_organisatorIds': userId}}, (error: MongoError, result: UpdateWriteOpResult) => {
db.close();
callback(result.modifiedCount == 1);
});
} else {
db.collection('organisations').updateOne({'_id': new ObjectID(organisationId)}, {$push: {'_memberIds': userId}}, (error: MongoError, result: UpdateWriteOpResult) => {
db.close();
callback(result.modifiedCount == 1);
});
}
});
示例5: callback
this._client.connect(DaoConstants.CONNECTION_URL, (err:any, db:Db) => {
db.collection('circlesessions').insertOne(circleSession, (err:MongoError, res:InsertOneWriteOpResult) => {
circleSession._id = res.insertedId.toString();
db.close();
callback(circleSession);
});
});
示例6: ObjectID
this._client.connect(DaoConstants.CONNECTION_URL, (err:any, db:Db) => {
db.collection('circlesessions').updateOne({_id: new ObjectID(circleSessionId)},
{$set:{_inProgress: inProgress}}, (err:MongoError, res:UpdateWriteOpResult) => {
db.close();
callback();
});
});
示例7: callback
this._client.connect(DaoConstants.CONNECTION_URL, (err: any, db: Db) => {
db.collection('groups').find({'_organisationId': organisationId}).toArray((err: MongoError, docs: Group[]) => {
db.close();
callback(docs);
});
});
示例8: ObjectID
this._client.connect(DaoConstants.CONNECTION_URL, (err: any, db: Db) => {
db.collection('groups').deleteOne({'_id': new ObjectID(groupId)}, (err: MongoError, result: DeleteWriteOpResultObject) => {
db.close();
callback(result.deletedCount == 1);
});
});
示例9: findArray
/**
* ドキュメントを検索して複數の項目を取得します
*/
async findArray(collectionName: string, query: { [x: string]: any }, options?: IFindArrayOptions): Promise<any[]> {
options = options || {};
if (options.since != null || options.until != null) {
query._id = {};
}
if (options.since != null) {
query._id.$gt = options.since;
}
if (options.until != null) {
query._id.$lt = options.until;
}
let cursor = this.db.collection(collectionName).find(query);
if (options.limit != null)
cursor = cursor.limit(options.limit);
if (options.isAscending != null)
cursor = cursor.sort(MongoProvider.buildSortOption(options.isAscending));
const documents = await cursor.toArray();
return documents;
}