做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Mongoose中document與object的區別示例詳解

來源:懂視網 責編:小采 時間:2020-11-27 22:29:43
文檔

Mongoose中document與object的區別示例詳解

Mongoose中document與object的區別示例詳解:前言 本文主要給大家總結介紹了關于Mongoose中document與object區別的相關內容,分享出來供大家參考學習,其實這個問題其實是mongoose非常常見的問題,經常有很多以前沒遇到這個問題的人都會被這個問題弄得懷疑人生。 我們先介紹一些問題的背景。 先看下面一
推薦度:
導讀Mongoose中document與object的區別示例詳解:前言 本文主要給大家總結介紹了關于Mongoose中document與object區別的相關內容,分享出來供大家參考學習,其實這個問題其實是mongoose非常常見的問題,經常有很多以前沒遇到這個問題的人都會被這個問題弄得懷疑人生。 我們先介紹一些問題的背景。 先看下面一

前言

本文主要給大家總結介紹了關于Mongoose中document與object區別的相關內容,分享出來供大家參考學習,其實這個問題其實是mongoose非常常見的問題,經常有很多以前沒遇到這個問題的人都會被這個問題弄得懷疑人生。

我們先介紹一些問題的背景。

先看下面一段代碼:

router.get('/', function(req, res, next) {
 // res.render('index', { title: 'Express' });
 const model = mongoose.model('realestate');
 const queryCretia = {};
 model.find(queryCretia, (err, docs) => {
 res.render('index', {
 title: 'express',
 docs: docs
 })
 })
});
<!DOCTYPE html>
<html>
 <head>
 <title><%= title %></title>
 <link rel='stylesheet' href='/stylesheets/style.css' />
 </head>
 <body>
 <h1><%= title %></h1>
 <p>Welcome to <%= title %></p>
 <!-- <%= docs %> -->
 <ul>
 <% docs.forEach(function(doc){ %>
 <li><%= doc.type %></li>
 <% }) %>
 </ul>
 </body>
</html>

在第一段代碼中,通過model.find我們應該能夠獲取到根據queryCriteria獲取的結果,結果應該是一個對象數組,類似于這樣:

[{
 "_id" : ObjectId("59bdeadb2a5c612514ee7970"),
 "title" : "好樓層,中等裝修,滿5年,上門實拍",
 "type" : "2室1廳",
 "square" : "75.42平",
 "direction" : "朝南",
 "floor" : "中區/6層",
 "unitPrice" : 47732,
 "totalPrice" : 360,
 "location" : null,
 "specialExplain" : "滿五",
 "url" : "http://sh.lianjia.com//ershoufang/sh4528035.html",
 "station" : "江楊北路",
 "line" : "3號線",
 "updateTime" : "2017-09-17 11:24:11"
}
{
 "_id" : ObjectId("59bdeadb2a5c612514ee7971"),
 "title" : "南北戶型,廚衛全明,高區采光好,裝修精美",
 "type" : "2室2廳",
 "square" : "90.92平",
 "direction" : "朝南北",
 "floor" : "高區/6層",
 "unitPrice" : 46194,
 "totalPrice" : 420,
 "location" : null,
 "specialExplain" : "滿五",
 "url" : "http://sh.lianjia.com//ershoufang/sh4546221.html",
 "station" : "江楊北路",
 "line" : "3號線",
 "updateTime" : "2017-09-17 11:24:11"
}]

預期index.ejs應該渲染的頁面是一個ul渲染的結果,類似于

  • 2室1廳
  • 2室2廳
  • 當然,理想很豐滿,現實很骨感。我就是死活渲染不出來doc.type。照理說應該是不可能的,在index.ejs中doc就是一個對象,我為什么不能獲取doc的type屬性呢?這不合理,太不合理了!

    老實說,這個問題我之間也是遇到過,當初我是想修改這個doc的屬性,但是死活沒有辦法修改,當初也是花了很久找到原因。這次我就把這個問題好好地研究一下。

    先說結果,以及解決方法把。我比較喜歡劇透。愿意是因為再次返回的doc是屬于Document的實例,而不是一個普通的對象。也就是說它和普通的對象是不一樣的,它沒有普通對象的一些方法,普通對象也沒有它身上的一些方法。

    解決方案有幾種,不過究其根本都是將這種document轉化為普通的對象:

    方法1:

    利用toObject方法

    docs.forEach(doc => {
     return doc.toObject();
    })

    方法2:

    利用JSON方法,這是我想到的一個方法,具體深層原因在這就不展開了:

    docs = JSON.stringify(docs);
    docs = JSON.parse(docs);

    方法3:

    利用lean方法:

    model.find().lean().exec((err, docs) => {
    ....
    })

    上述的三種方法應該都能成功將find獲取的結果轉化為普通的對象。

    但是我還想知道到底document和一個普通的對象到底有什么區別,區別在哪里呢?

    我們假設find獲取的結果是docs,轉化為普通對象的結果是docs1。現在我們就看一看這二者的區別。理論上docs和docs1都應該是數組,而它們中元素都應該是一個對象,我們先來看看是不是這樣呢?

    console.log(Object.prototype.toString.call(docs));
    consoele.log(Object.prototype.toString.call(docs[0]));
    
    console.log(Object.prototype.toString.call(docs1));
    console.log(Object.prototype.toString.call(docs1[0]))

    我們通過上述方法可以獲取docs以及docs1的類型以及其中元素的類型,結果是:

    [object Array]
    [object Object]
    
    [object Array]
    [object Object]

    和我們預想中的一模一樣,那問題不在這,那我們就探究探究docs[0]以及docs1[0]的原型把,看看它的原型到底是什么呢?知道JS的人,應該都知道JS中的原型鏈。在此,我們就通過__proto__來粗暴地獲取對象的原型:

    console.dir(doc[0].__proto__);
    
    console.dir(docs[0].__proto__);

    結果是:

    model {
     db:
     NativeConnection {
     base:
     Mongoose {
     connections: [Array],
     models: [Object],
     modelSchemas: [Object],
     options: [Object],
     plugins: [Array] },
     collections: { realestates: [Object] },
     models: { realestate: [Object] },
     config: { autoIndex: true },
     replica: false,
     hosts: null,
     host: '127.0.0.1',
     port: 27017,
     user: undefined,
     pass: undefined,
     name: 'real_estate_info',
     options:
     { db: [Object],
     auth: {},
     server: [Object],
     replset: [Object],
     mongos: undefined },
     otherDbs: [],
     _readyState: 1,
     _closeCalled: false,
     _hasOpened: true,
     _listening: false,
     db:
     Db {
     domain: null,
     _events: [Object],
     _eventsCount: 6,
     _maxListeners: undefined,
     s: [Object],
     serverConfig: [Getter],
     bufferMaxEntries: [Getter],
     databaseName: [Getter],
     _listening: true },
     _events:
     { connected: [Function],
     error: [Function: bound bound consoleCall],
     disconnected: [Function: bound bound consoleCall],
     reconnected: [Function: bound bound consoleCall] },
     _eventsCount: 4 },
     discriminators: undefined,
     id: [Getter/Setter],
     __v: [Getter/Setter],
     _id: [Getter/Setter],
     schema:
     Schema {
     obj: undefined,
     paths: { _id: [Object], __v: [Object] },
     aliases: {},
     subpaths: {},
     virtuals: { id: [Object] },
     singleNestedPaths: {},
     nested: {},
     inherits: {},
     callQueue: [ [Array], [Array], [Array], [Array], [Array], [Array] ],
     _indexes: [],
     methods: {},
     statics: {},
     tree: { _id: [Object], __v: [Function: Number], id: [Object] },
     query: {},
     childSchemas: [],
     plugins: [ [Object], [Object], [Object], [Object] ],
     s: { hooks: [Object], kareemHooks: [Object] },
     options:
     { retainKeyOrder: false,
     typeKey: 'type',
     id: true,
     noVirtualId: false,
     _id: true,
     noId: false,
     validateBeforeSave: true,
     read: null,
     shardKey: null,
     autoIndex: null,
     minimize: true,
     discriminatorKey: '__t',
     versionKey: '__v',
     capped: false,
     bufferCommands: true,
     strict: true,
     pluralization: true },
     '$globalPluginsApplied': true,
     _requiredpaths: [] },
     collection:
     NativeCollection {
     collection: Collection { s: [Object] },
     opts: { bufferCommands: true, capped: false },
     name: 'realestates',
     collectionName: 'realestates',
     conn:
     NativeConnection {
     base: [Object],
     collections: [Object],
     models: [Object],
     config: [Object],
     replica: false,
     hosts: null,
     host: '127.0.0.1',
     port: 27017,
     user: undefined,
     pass: undefined,
     name: 'real_estate_info',
     options: [Object],
     otherDbs: [],
     _readyState: 1,
     _closeCalled: false,
     _hasOpened: true,
     _listening: false,
     db: [Object],
     _events: [Object],
     _eventsCount: 4 },
     queue: [],
     buffer: false,
     emitter:
     EventEmitter {
     domain: null,
     _events: {},
     _eventsCount: 0,
     _maxListeners: undefined } },
     '$__original_save': { [Function] numAsyncPres: 0 },
     save: { [Function: wrappedPointCut] '$originalFunction': '$__original_save', '$isWrapped': true },
     _pres:
     { '$__original_save': [ [Object], [Object], [Object] ],
     '$__original_remove': [ [Object] ] },
     _posts: { '$__original_save': [], '$__original_remove': [] },
     '$__original_remove': { [Function] numAsyncPres: 1 },
     remove:
     { [Function: wrappedPointCut]
     '$originalFunction': '$__original_remove',
     '$isWrapped': true },
     '$__original_validate': [Function],
     validate:
     { [Function: wrappedPointCut]
     '$originalFunction': '$__original_validate',
     '$isWrapped': true } }

    以及

    {}

    很顯然,問題就是在這里,docs[0]和docs[0]的原型并不是一個東西。而js中對象通過.或者是[]訪問屬性都是調用了Object中的某個方法,但具體什么方法我不太記得。然而docs中的原型或者其原型的原型也是沒有這個方法的,因此他就沒辦法去訪問這個屬性。

    其實docs[0].__proto__.__proto__是Model,docs[0].__proto__.__proto__.__proto__是Document,docs[0].__proto__.__proto__.__proto__.__proto__才是{}。

    至此,這個問題引起的一系列的探究也是告一段落了。其實Mongoose還有另外一些奇怪的地方,被人所詬病,在此也不一一細數了。從問題的發現,到寫這篇文章大概花了大半天的時間,以前遇到問題就找到解決辦法就停止了,但是這一次通過這樣深入地去發現,可能就會發掘到更多的東西。

    總結

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    Mongoose中document與object的區別示例詳解

    Mongoose中document與object的區別示例詳解:前言 本文主要給大家總結介紹了關于Mongoose中document與object區別的相關內容,分享出來供大家參考學習,其實這個問題其實是mongoose非常常見的問題,經常有很多以前沒遇到這個問題的人都會被這個問題弄得懷疑人生。 我們先介紹一些問題的背景。 先看下面一
    推薦度:
    標簽: 示例 實例 的實例
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 鞍山市| 隆尧县| 随州市| 济南市| 青龙| 亳州市| 德江县| 额尔古纳市| 横峰县| 陆良县| 巨鹿县| 曲周县| 五莲县| 绥江县| 裕民县| 土默特左旗| 龙海市| 岳阳市| 成都市| 昭苏县| 策勒县| 家居| 仪征市| 恩施市| 抚松县| 杨浦区| 铜鼓县| 获嘉县| 辉县市| 故城县| 杭锦旗| 河北区| 屏边| 景谷| 郯城县| 白沙| 比如县| 洪雅县| 泰安市| 海伦市| 襄垣县|