ThinkJS中MongoDB的CURD操作是什嗎?CURD操作的使用教程

來源:互聯網
上載者:User

前言

眾所周知目前使用Node.js + mongodb已經成為很多公司的技術棧。ThinkJS其實也提供了對mongo的支援,雖然官方文檔較少,但是保證了ORM的API的一致性,所以用起來需要查看基本的>Model api

基本的模型檔案放在common/model下

擷取列表

getList(q, page) {
returnthis.select();
}

分頁加條件搜尋

search(q, page) {
if(q) {
q =newRegExp(q,'i');
}
returnthis.where({'name':{ $regex: q}}).page(page, 20).countSelect();
}

擷取詳情

getDetail(id) {
returnthis.where({'_id':id}).select();
}

建立資料

addTag(tag) {
returnthis.add(tag);
}

更新資料

updateTag(id,data) {
returnthis.where({'_id':id}).update(data);
}

刪除資料

removeOne(id) {
returnthis.where({'_id':id}).delete();
}

這樣你需要什麼只需要調用相應模型的方法就好了。

一個簡單的介面如下:

async createAction() {
let name =this.post('name');
let contents =this.post('contents');
// 擷取模型執行個體
let m =this.model('tag');
let res = await m.addTag({
'name': name,
'contents': contents,
'count': 0,
});
if(res) {
this.success('');
}else{
this.apiErrorHandle(4000);
}
}
....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.