SoFunction
Updated on 2025-03-09

How to use MongoDB's CURD operation in ThinkJS

Preface

As we all know, using +mongodb has become the technology stack for many companies. ThinkJS actually provides support for mongo, althoughOfficial DocumentationFewer, but ensures consistency of ORM's API, so you need to check the basic>Model api

The basic model file is placed under common/model

Get the list

getList(q, page) { 
 return ();
 }

Pagination plus conditional search

search(q, page) { 
 if(q) {
  q = new RegExp(q,'i');
 }
 return ({'name':{ $regex: q}}).page(page, 20).countSelect();
 }

Get details

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

Create data

addTag(tag) { 
 return (tag);
 }

Update data

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

Delete data

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

In this way, you just need to call the corresponding model method.

A simple interface is as follows:

async createAction() { 
 let name = ('name');
 let contents = ('contents');
 // Get the model instance let m = ('tag');
 let res = await ({
  'name': name,
  'contents': contents,
  'count': 0,
 });
 if(res) {
  ('');
 }else{
  (4000);
 }
 }
 ....

Summarize

The above is the entire content of this article. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.