mongodb用mongoose取到的對象不能增加屬性

來源:互聯網
上載者:User

標籤:

先定義了一個article的schema

var mongoose = require(‘mongoose‘);var Schema = mongoose.Schema;exports.schema = new Schema({    title: String,//標題    description: String,//描述    content: String,//內容   status:{type: Number, defalut: 0}, //未發布:0 ,發布:1   create_at: {type: Date, default: Date.now}//添加時間 });

增加一條測試資料:

var o = new articleModel();o.title = ‘hello‘;o.content = ‘這是一篇測試文章‘;o.save(function(err,result){    if(err){        console.log(err.message);    }    console.log(result);});

下面使用findOne方法擷取這條記錄,在擷取到的記錄上增加一個remark屬性,並在控制台輸出結果

articleModel.findOne({title: ‘hello‘}, function (err, article) {    article.remark = ‘備忘‘;    console.log(err, article);});結果:{  "content":"這是一篇測試文章",  "title":"hello",  "_id":"56f5ee83fcfad37f1371e952",  "__v":0,   "status":0,  "create_at":"2016-03-26T02:05:55.814Z"}

發現結果中remark屬性沒有顯示,同時在schema中聲明過的description也沒有顯示(因為增加資料的時候就沒有設定description的值)。我現在把description也設定一個值看看查詢結果:

articleModel.findOne({title: ‘hello‘}, function (err, article) {    article.remark = ‘備忘‘;    article.description = ‘這是描述‘;    console.log(err, article);});結果:{"description":"這是描述",   "content":"這是一篇測試文章",   "title":"hello",   "_id":"56f5ee83fcfad37f1371e952",   "__v":0,   "status":0,   "create_at":"2016-03-26T02:05:55.814Z" }

我們在發現description賦值成功,但是增加的remark屬性還是無效。

這是為什麼呢?因為Mongoose是個ODM (Object Document Mapper),類似於操作關係型資料庫使用的ORM(Object Relational Mapper),我們使用Mongoose取到的資料的結構是要依賴於我們定義的schema結構的。增加的remark屬性在schema中沒有定義,所以我們在取到的結果中增加remark屬性是無效的,而description屬性先前在結構中有定義(不算新增),所以可以重新設定值。

結論:mongodb中使用mongoose取到的對象不能增加屬性。

接著問題是,如果我需要在結果中補充新的屬性使用怎麼辦?

方法1、在schema中直接增加需要補充的屬性。

exports.schema = new Schema({    title: String,//標題    description: String,//描述    content: String,//內容        remark:String, //備忘(補充新屬性,現在和description一樣了)    create_at: {type: Date, default: Date.now}//添加時間});

方法2、把查詢到的結果clone一個對象,然後在新對象中補充屬性。

articleModel.findOne({title: ‘hello‘}, function (err, article) {  var newobj = null;  if(article){      newobj = {        _id:article._id,        title: article.title,//標題        description: article.description,//描述        content: article.content,//內容        remark:"備忘",        create_at: article.create_at,     status: article.status,     status_name: article.status==1?‘發布‘:‘未發布‘;    };  }    console.log(newobj); });

方法3:像上面的例子在schema中已經有了status表示狀態,如果我們僅僅需要一個status_name顯示文章狀態的中文解釋。不要clone新對象的方式,可以使用schema的虛擬屬性。

聲明修改一下如下:var mongoose = require(‘mongoose‘);var Schema = mongoose.Schema;var schema = new Schema({    title: String,//標題    description: String,//描述    content: String,//內容    status: {type: Number, defalut: 0}, //未發布:0 ,發布:1    create_at: {type: Date, default: Date.now}//添加時間});schema.virtual(‘status_name‘).get(function () {    return this.status == 1 ? ‘發布‘ : ‘未發布‘;});exports.schema = schema;查詢到結果後直接用可以直接使用status_name屬性:articleModel.findOne({title: ‘hello‘}, function (err, article) {    console.log(article.stauts_name); });

 

mongodb用mongoose取到的對象不能增加屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.