2016-09-14 23 views
7

Şeması güncellenmiyorsa Zaman damgaları:MongoDB/Gelincik

var schema = new Schema({...}, { 
    timestamps: true, 
    id: false, 
    toJSON: { 
     virtuals: true, 
    }, 
    toObject: { 
     virtual: true, 
    } 
}); 
schema.virtual('updated').get(function() { 
    if(typeof this.updatedAt === "undefined" && typeof this.createdAt === "undefined") return ""; 
    var updated = (typeof this.updatedAt === "undefined") ? this.createdAt : this.updatedAt; 
    return "Updated "+moment(updated).fromNow(); 
}); 

Bu kod son zamanlarda çalışan oldu - updatedAt belirli örneği için 24 Ağustos olarak çıkageldi, ancak herhangi bir yeni düzenleme belgeye güncellemesi yok zaman damgası.

Burada çok saçma bir şey eksik gibi hissediyorum.

var schema =new Schema({..}, 
      { timestamps: { createdAt: 'createdDate',updatedAt: 'updatedDate' } 
}); 

için: koşul false hep

schema.virtual('updated').get(function() { 
    if(typeof this.updatedAt === undefined && typeof this.createdAt === undefined) return ""; 
    var updated = (typeof this.updatedAt === undefined) ? this.createdAt : this.updatedAt; 
    return "Updated "+moment(updated).fromNow(); 
}); 

, bu

+0

Bu.updatedAt türünü kontrol edebilir misiniz? – abdulbarik

+0

@abdulbarik typeof league.updatedAt => object –

+0

Kopyaladığınız kodu kopyalayıp sunucumda çalışıyorum ve mongoose 4.6.1 ile gayet iyi çalışıyor, Yani başka bir yerde eksik olabilirsiniz. Lütfen kullandığınız mongoose sürümünü veya herhangi bir fongoose eklentisini belirtin. –

cevap

0

, işte Bu şema zaman aşımları save(), update() ve findOneAndUpdate()'da güncellenecektir . böylece gerek schema.virtual('updated')...

Süreci-2

sizin şemada Date tip createdDate ve updatedDate eklenmiş ve şema eklentisi kullanarak bu tarih alanlarını güncelleştirmek.

gibi:

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    SchemaPlugin = require('../helpers/schemaPlugin'); 
    var schema =new Schema({..}, 
    createdDate: { 
     type: Date, 
     default: Date.now 
    }, 
    updatedDate: { 
     type: Date, 
     default: Date.now 
    } 
    }); 

    schema.plugin(SchemaPlugin); 

dosya schemaPlugin.js yılında:

module.exports = function(schema) { 

    var updateTimestemps = function(next){ 
    var self = this; 


    if(!self.createdAt) { 
     self.createdDate = new Date(); 
     //or self.update({},{ $set: { createdDate : new Date(), updatedDate: new Date() } }); 
    } else { 
     self.updatedDate= new Date(); 
     //or self.update({},{ $set: {updatedDate: new Date() } }); 
    } 
    next(); 
    }; 

    schema. 
    pre('save', updateTimestemps). 
    pre('update', updateTimestemps). 
    pre('findOneAndUpdate', updateTimestemps); 
}; 
+0

Cevabınız için teşekkürler, ancak sorun bu bölümle birlikte görünmüyor - sorun, updateAt zaman damgasındaki düzenleme ile güncellenmiyor. –

+0

, durumunuz iyi çalışıyor mu? – abdulbarik

+0

hangi yerde takıldınız? – abdulbarik

2

tarafından denemek gibi şema değiştirebilir çalışmalıdır bu deneyin olmasının nedenini String ile object karşılaştırdığınız

1
böylece bir çek updatedAt updatedAt ve createdAt aynı zamanda oluşturulan hem yeni belge mongoose kullanarak veritabanına girildiğinde ise

olsun Yeni belge oluşturulduğunda her ikisi de aynı değere sahip olacağı için tanımsız veya mantıksızdır.

Ne zaman bir mongoose güncelleme işlevi veya findByIdAndUpdate veya findOneAndUpdate kullandığınızda updateAt değeri otomatik olarak güncellenecektir Mongodb istemcisi mongochef veya robomongo gibi updateAt değerini doğrudan kontrol etmek için kullanılır.