Vueファミリーバケツ+SSR+Koa 2全桟開発美団網③——mongose基礎
3608 ワード
まずmongodbをインストールして、データベースを起動します
次にmongodb可視化データ管理ツールRobo 3 Tをインストールする
やはりkoa 2プロジェクトの下でmongoseをインストールします
koa 2ルートディレクトリの下にdbsフォルダを新規作成し、このフォルダの下にプロファイルconfig.jsファイルを新規作成し、書き込む
次にdbsフォルダの下にmodelsフォルダを新規作成し、このフォルダの下にプロファイルperson.jsファイルを新規作成し、書き込み
次に、app.jsにこのモデルと構成をサーバに導入する必要があります.
その後、データベース・サービスを開き、27017にアクセスし、プロジェクト・ルート・ディレクトリの下でnpm run devアクセスlocalhost:3000にアクセスすれば正常です.
モデルを定義しましたが、データがありませんので、サーバはデフォルトで表示されません.次に、データの削除を実行します.
routerフォルダの下のuser.jsにpersonモデルを導入して使用し、書き込みます.
git bashコマンドラインで次のコマンドを実行し、postでデータの追加を要求します.
コマンドラインは{code:0}を返してデータの追加に成功し、Robo 3 Tを開くとデータベースとデータが表示されます.
考え直す
まずデータベースにはデータテーブルが必要です.modelsフォルダの下にperson.jsファイルを作成しました.このファイルは対応するデータテーブルです.
次に、このテーブルの下にあるデータを作成するには、mogooseを使用して{name:String,age:Number}などのデータを記述するSchemaテーブルを作成します.
しかし、Schemaテーブルは声明にすぎませんが、modelをブリッジとして構築することで、データテーブルにデータを関連付け、Schemaテーブルにも関連付ける必要があります.
しかし、modelはブリッジにすぎず、インスタンスではありませんが、インスタンス化が必要なので、ルーティングでクラスをインスタンス化し、modelのsaveストレージ方法とルーティングインタフェースを通じてデータを書きます.
上は書き込みの方法だけですが、次に読み出しの方法を作成し、ルーティングに注意してください.
git bashコマンドラインで次のコマンドを実行すると、データを取得できます.
次に、変更方法を作成します.
git bashコマンドラインで次のコマンドを実行すると、データを変更できます.
削除方法の作成
git bashコマンドラインで次のコマンドを実行すると、データを削除できます.
次にmongodb可視化データ管理ツールRobo 3 Tをインストールする
やはりkoa 2プロジェクトの下でmongoseをインストールします
npm i mongoose
koa 2ルートディレクトリの下にdbsフォルダを新規作成し、このフォルダの下にプロファイルconfig.jsファイルを新規作成し、書き込む
module.exports = {
// dbs
dbs: 'mongodb://127.0.0.1:27017/dbs'
}
次にdbsフォルダの下にmodelsフォルダを新規作成し、このフォルダの下にプロファイルperson.jsファイルを新規作成し、書き込み
const mongoose = require('mongoose')
//
let personSchema = new mongoose.Schema({
name: String,
age: Number
})
// Person
module.exports = mongoose.model('Person', personSchema)
次に、app.jsにこのモデルと構成をサーバに導入する必要があります.
const mongoose = require('mongoose')
const dbconfig = require('./dbs/config')
//
mongoose.connect(dbconfig.dbs, {
useNewUrlParser: true
})
その後、データベース・サービスを開き、27017にアクセスし、プロジェクト・ルート・ディレクトリの下でnpm run devアクセスlocalhost:3000にアクセスすれば正常です.
モデルを定義しましたが、データがありませんので、サーバはデフォルトで表示されません.次に、データの削除を実行します.
routerフォルダの下のuser.jsにpersonモデルを導入して使用し、書き込みます.
const router = require('koa-router')()
const Person = require('./../dbs/models/person')
router.prefix('/users')
router.get('/', function (ctx, next) {
ctx.body = 'this is a users response!'
})
router.get('/bar', function (ctx, next) {
ctx.body = 'this is a users/bar response'
})
router.post('/addPerson', async function (ctx, next) {
const person = new Person({
name: ctx.request.body.name,
age: ctx.request.body.age
})
let code
try {
await person.save()
code = 0
}
catch (e) {
code = -1
}
ctx.body = {
code: code
}
})
module.exports = router
git bashコマンドラインで次のコマンドを実行し、postでデータの追加を要求します.
curl -d 'name=lilei&age=27' http://localhost:3000/users/addPerson
コマンドラインは{code:0}を返してデータの追加に成功し、Robo 3 Tを開くとデータベースとデータが表示されます.
考え直す
まずデータベースにはデータテーブルが必要です.modelsフォルダの下にperson.jsファイルを作成しました.このファイルは対応するデータテーブルです.
次に、このテーブルの下にあるデータを作成するには、mogooseを使用して{name:String,age:Number}などのデータを記述するSchemaテーブルを作成します.
しかし、Schemaテーブルは声明にすぎませんが、modelをブリッジとして構築することで、データテーブルにデータを関連付け、Schemaテーブルにも関連付ける必要があります.
しかし、modelはブリッジにすぎず、インスタンスではありませんが、インスタンス化が必要なので、ルーティングでクラスをインスタンス化し、modelのsaveストレージ方法とルーティングインタフェースを通じてデータを書きます.
上は書き込みの方法だけですが、次に読み出しの方法を作成し、ルーティングに注意してください.
router.post('/getPerson', async function (ctx) {
const result = await Person.findOne({name: ctx.request.body.name})
const results = await Person.find({name: ctx.request.body.name})
ctx.body = {
code: 0,
result,
results
}
})
git bashコマンドラインで次のコマンドを実行すると、データを取得できます.
curl -d 'name=lilei' http://localhost:3000/users/getPerson
次に、変更方法を作成します.
router.post('/updatePerson', async function (ctx) {
const result = await Person.where({name: ctx.request.body.name}).update({age: ctx.request.body.age})
ctx.body = {
code: 0
}
})
git bashコマンドラインで次のコマンドを実行すると、データを変更できます.
curl -d 'name=lilei&age=30' http://localhost:3000/users/updatePerson
削除方法の作成
router.post('/removePerson', async function (ctx) {
const result = await Person.where({name: ctx.request.body.name}).remove()
ctx.body = {
code: 0
}
})
git bashコマンドラインで次のコマンドを実行すると、データを削除できます.
curl -d 'name=lilei' http://localhost:3000/users/removePerson