folder Tahribat.com Forumları
linefolder Database - Veritabanı
linefolder Mongo DB Join - Populate De Require Kullanımı



Mongo DB Join - Populate De Require Kullanımı

  1. KısayolKısayol reportŞikayet pmÖzel Mesaj
    dalyKadir
    dalyKadir's avatar
    Kayıt Tarihi: 22/Haziran/2020
    Erkek

    Selamlar, Elimde bir proje var. Hiç bilmediğim bir MongoDB projesine girdim, işin içinden cikamadim.

    Yapmak istediğim aşağıda ki populate (media'nin) 'de kayit yok ise o record hiç gelmesin. (Düz sql de ki, Where is not null)

    Burası zorunlu alan olsun:
    .populate({ path: 'media', select: '_id name' })

    Bunun nasil oldugunu bilen varsa bi sağlam hayır dua ederim.

    Kod:

    let items = await DB.Webinar.find(query, { availableTimeRange: 0, slotIds: 0, hashWebinar: 0 })
    .populate({ path: 'categories', select: '_id name alias' })
    .populate({ path: 'media', select: '_id name' })
    .sort(sort)
    .skip(page * take)
    .limit(take)
    .exec();

     

    Model:

    /* eslint prefer-arrow-callback: 0 */
    const Schema = require('mongoose').Schema;

    const schema = new Schema(
    {
    tutorId: {
    type: Schema.Types.ObjectId,
    index: true,
    ref: 'User'
    },
    name: {
    type: String,
    default: ''
    },
    alias: {
    type: String,
    index: true
    },
    categoryIds: [
    {
    type: Schema.Types.ObjectId,
    index: true,
    ref: 'Category'
    }
    ],
    mediaIds: [
    {
    type: Schema.Types.ObjectId,
    index: true,
    ref: 'Media'
    }
    ],
    mainImageId: {
    type: Schema.Types.ObjectId,
    ref: 'Media',
    default: null
    },
    createdAt: {
    type: Date
    },
    updatedAt: {
    type: Date
    },
    .........
    },
    {
    timestamps: {
    createdAt: 'createdAt',
    updatedAt: 'updatedAt'
    },
    toJSON: {
    virtuals: true
    },
    toObject: {
    virtuals: true
    }
    }
    );
    schema.virtual('media', {
    ref: 'Media',
    localField: 'mediaIds',
    foreignField: '_id',
    justOne: false
    });
    schema.virtual('mainImage', {
    ref: 'Media',
    localField: 'mainImageId',
    foreignField: '_id',
    justOne: true
    });

    module.exports = schema;
  2. KısayolKısayol reportŞikayet pmÖzel Mesaj
    yolbulucu
    yolbulucu's avatar
    Kayıt Tarihi: 06/Nisan/2007
    Homo

    tam ne yapmaya çalıştığını anlamadım ama yazığın kodda mongodb ile ilgili hiç bir şey yok. tamamen mongoose kodu.

    mongodb den tam olarak ne yapmaya çalıştığını anlatırsan yardımcı olabilirim belki.

    hunharca mongodb kullanıyorum ama mongoose kullanmadım hiç


    29.99cm, titanyum kaplamalı, çift damarlı, su ve yağ soğutmalı.
  3. KısayolKısayol reportŞikayet pmÖzel Mesaj
    Elif - Efe
    rakkoc
    rakkoc's avatar
    Kayıt Tarihi: 19/Aralık/2003
    Homo

    @dalyKadir lan göt, adam soruna cevap vermiş dönüp bakmamişsin. Bana look up olayını o anlattı :)


    Bu aralar fazla yogunum. Unuttugum seyler oluyorsa affola. DM vs atın.
Toplam Hit: 1074 Toplam Mesaj: 3
nodejs mongo mongoose