国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:151日期: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)文章:
主站蜘蛛池模板: 潜江市| 田林县| 宽城| 象州县| 衡山县| 伊宁县| 龙江县| 图木舒克市| 康乐县| 四会市| 天柱县| 任丘市| 吉安市| 拉孜县| 益阳市| 迭部县| 宜春市| 姜堰市| 沁源县| 广德县| 柘城县| 涟水县| 潞城市| 辽阳县| 汉川市| 朝阳县| 逊克县| 商洛市| 达尔| 个旧市| 鸡东县| 石城县| 聂拉木县| 丁青县| 罗甸县| 平原县| 长治县| 石渠县| 磴口县| 工布江达县| 东明县|