成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

node.js - 如何使用mongoose連接數(shù)據(jù)庫中已經(jīng)存在的一個集合。

瀏覽:138日期:2024-07-20 14:49:28

問題描述

問題解答

回答1:

要想使用mongooes來連接mongo數(shù)據(jù)庫中已有的一個數(shù)據(jù)集合,需要在定義模式的時候加一個參數(shù){ collection: '集合名' },這里的集合名是數(shù)據(jù)庫中已有的集合。如下:node.js - 如何使用mongoose連接數(shù)據(jù)庫中已經(jīng)存在的一個集合。

之后定義模型的時候和之前是一樣的:node.js - 如何使用mongoose連接數(shù)據(jù)庫中已經(jīng)存在的一個集合。

這里的第三個參數(shù)是解決在數(shù)據(jù)庫中集合名會自動變?yōu)閺?fù)數(shù)的問題

回答2:

mongoose從數(shù)據(jù)庫讀取數(shù)據(jù), 不需要mongoose.collection(’collectionName’).完整的學(xué)習(xí)參考mongoose文檔。簡單例子如下,其中{}是具體條件或者數(shù)據(jù)。-- model.js --

const mongoose = require(’mongoose’);mongoose.connect(’mongodb://locahost/dbName’)const dataSchema = new mongoose.Schema({});const dataModel = mongoose.model(’modelName’, dataSchema, ’collectionName’);module.exports = dataModel;

-- CRUD data --

let dataModel = require(’./model.js’);dataModel.create({}, cb);dataModel.find({}, cb);dataModel.update({}, {}, cb);dataModel.remove({}, cb);

soonfy

回答3:

請參看一下Mongoose的文檔的相關(guān)章節(jié):

http://mongoosejs.com/docs/qu...

供參考。

Love MongoDB! Have Fun!

相關(guān)文章: