node.js - 如何使用mongoose連接數(shù)據(jù)庫中已經(jīng)存在的一個集合。
問題描述
問題解答
回答1:要想使用mongooes來連接mongo數(shù)據(jù)庫中已有的一個數(shù)據(jù)集合,需要在定義模式的時候加一個參數(shù){ collection: '集合名' },這里的集合名是數(shù)據(jù)庫中已有的集合。如下:
之后定義模型的時候和之前是一樣的:
這里的第三個參數(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)文章:
1. javascript - 為什么我的vue里的router-link不起作用2. javascript - 求助Angular 跨控制器調(diào)用方法可行嗎?3. CSS更改未得到反映。為什么?4. 用戶在微信小程序支付成功以后,財(cái)務(wù)在微信支付后臺用交易單號能查到這筆訂單 但財(cái)務(wù)說錢沒有入對公賬號?5. javascript - node redirect重定向失敗6. javascript - JS使用ele.style.backgoundImage = ’’ =’none’取消背景圖片都無效7. a標(biāo)簽跨域下載文件能否重命名?8. android - weex 項(xiàng)目createInstanceReferenceError: Vue is not defined9. javascript - 如圖,百度首頁,查看源代碼為什么什么都沒有?10. pdo 寫入到數(shù)據(jù)庫的內(nèi)容為中文的時候?qū)懭雭y碼
