اذهب إلى المحتوى
  • 0

كيفية عدم تخزين العناصر الفارغة في mongoDB

Samer Alashqar

السؤال

السلام عليكم..

كيف اجعل mongoDB يقوم بتجاهل تخزين القيم الفارغة و عبارات null في قاعدة البيانات ؟

proQuantity:null
proPrice:null
proDisPrice:null
proDescription:""

هذا ما قمت بفعله لكن لا اشعر انها الطريقة الصحيحة 100% هل يوجد حل افضل ؟

userSchema.pre('save', function(next) {
    this.proPromoName = this.proPromoName ? this.proPromoName: undefined;
    this.proDisPrice = this.proDisPrice ? this.proDisPrice: undefined;
    this.proQuantity = this.proQuantity ? this.proQuantity: undefined;
    
    next();
 })

 

تم التعديل في بواسطة Samer Alashqar
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

أعتقد من الأفضل تخزين سلسلة نصية فارغة 

userSchema.pre('save', function(next) {
    this.proPromoName = this.proPromoName ? this.proPromoName: "";
    this.proDisPrice = this.proDisPrice ? this.proDisPrice:    "";
    this.proQuantity = this.proQuantity ? this.proQuantity:    "";
    
    next();
 })

ولكن من الممكن حذف المفتاح بشكل نهائي لكي لا يستهلك أي موارد أبداً..

يمكن المرور على الكائن و حذف أي خاصية فيه قيمتها null مثلاً

for(var key in myObject){
  if(myObject.hasOwnProperty(key)){
    if(myObject[key] == null) 
      delete myObject[key];
  }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...