Step by step to create a public account GPT intelligent customer service (5) Free cloud database

Lesson 5: Free cloud database

Welfare of wool party-free cloud database use

The use of databases is unavoidable in the study or completion of back-end languages ​​and project development. After working hard on a project, it is a bit worrying to find that you can only watch it by yourself. At this time, we want to spread the word about our results. , let’s play together, it is definitely inseparable from the online process. The conventional online method is to go to a third-party platform to purchase a cloud server, or to do intranet penetration, but both methods require a lot of money. Small vault.

This article teaches you how to use the cloud database Atlas for free

register account

  • Mongodb official website to register atlas
  • Mongodb official website address: https://www.mongodb.com/zh-cn
  • Mongoose official website address: http://www.mongoosejs.net/docs/subdocs.html

Official website location

Free version location

Free version location

  • Create an atlas cluster

Cluster configuration and selection

Cluster ok

  • Account access whitelist configuration

After the cluster is created, you need to configure the account information to use it. The default is read and write permissions. After configuring the access whitelist, only requests within the whitelist range can access. If it is for testing and learning, you can directly set it to allow all User prevention

  1. Create access account

Account configuration

  1. Set read and write permissions

Account configuration

  1. Set the access whitelist to allow all

Whitelist configuration

Connect to use

After the configuration is completed, we can use the cloud database provided by altas. Here we will introduce two methods:Visual tool connectionandnode connection

  1. nodejs connection

Here we will use mongoose to implement node connection

  • Get connection address

    Get the connection address from the Altas official website

connection address

Connection method

Connection method

  1. Visual tool connection

The visualization tool chosen here is studio3tthat you can directly search and download the tool without introducing it here.

Official website: https://studio3t.com/download-studio3t-free/

  • Create a new connection in studio and import the url

Connection method

  • After the import is successful, perform a connection test

Connection method

  • After passing the test, you can connect to the atlas cloud database.

Connection method

Okay, this article will be introduced here first, and you guys can go and pluck wool to your heart’s content.

Database usage

1. Link database

$ npm i mongoose
const mongoose = require('mongoose')

const dburl = `mongodb+srv://lurongtao8080:[email protected]/?retryWrites=true&w=majority`

// mongodb+srv://lurongtao:<password>@cluster0.u5fuuxe.mongodb.net/?retryWrites=true&w=majority

mongoose.connect(dburl, {
    
     useNewUrlParser: true, useUnifiedTopology: true })

var db = mongoose.connection

db.on('error',()=>{
    
    
  console.log("数据库连接失败")
})

db.once('open', function() {
    
    
  console.log('数据库连接成功')
})

2. Data model

const  mongoose = require("mongoose")

let userSchema = mongoose.Schema({
    
    
  wxOpenId: {
    
     type: String, require:  true }, // 微信的openid
  balance: {
    
      type: Number, default: 5 },     // 提问的次数
  msg: {
    
     type: Array, default: [] },          // 用户提问过的内容(隐私)
  model: {
    
     type: String, default: 'chatgpt' } // 存储用户选择的对话模式
})

let userModel = mongoose.model("user", userSchema)

module.exports = userModel

//录屏软件 https://www.apowersoft.com.cn/record-screen-pinzhuan?apptype=aps-pin

Guess you like

Origin blog.csdn.net/xianyu120/article/details/133344610