Build a personal blog with Node.js+mongodb (model directory) (iii)

Source: Internet
Author: User

The model catalog is primarily packaged with some frequently used methods for ease of use.

Setting.js file:

Very simple, just encapsulate a URL as a public, later change is easy to change.

Md5.js (not recommended):

Db.js file:

Db.js files mainly encapsulate some connection operations to the database, otherwise writing on router.js will result in very large code redundancy and the same code.

For database operations, the first step is of course to connect to the database:

__connectdb as an intrinsic function, convenient to be referenced.

Then connect to the database using the method provided by Mongoclient.connect, connect receives two parameters (address, callback function)

Increase:

Inserting data into a database, of course, cannot limit only one, and certainly multiple data insertions.

So the Insertone function receives three parameters (database name, JSON to store, callback function)

You must close the database every time you do an operation on the database and after you have done it.

Why? Because each connection has a certain amount of pressure on the database, the database will be a catastrophic card if these idle pressures are not released.

Check:

Here I am divided into two parts.

The first part (make a judgment, judge the number of arguments that are coming)

Part II (Query database):

By deleting:

Removed from the database. Receive parameters (database name, JSON to delete, callback function)

Each operation requires a callback function to close the database.

Repair:

The Updatemany method receives four parameters (database name, JSON to be modified, modified JSON, callback function)

Finally, you close the database.

Paging:

The data that is queried must be paged out. So this function is also available.

Above..

Build a personal blog with Node.js+mongodb (model directory) (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.