日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

MongoDB中實(shí)現(xiàn)多表聯(lián)查的實(shí)例教程_MongoDB

作者:山維空間 ? 更新時(shí)間: 2022-08-31 編程語(yǔ)言

前些天遇到一個(gè)需求,不復(fù)雜,用 SQL 表現(xiàn)的話(huà),大約如此:

SELECT *
FROM db1 LEFT JOIN db2 ON db1.a = db2.b
WHERE db1.userId='$me' AND db2.status=1

沒(méi)想到搜了半天,我廠的代碼倉(cāng)庫(kù)里沒(méi)有這種用法,各種教程也多半只針對(duì)合并查詢(xún)(即只篩選?db1,沒(méi)有?db2?的條件)。所以最后只好讀文檔+代碼嘗試,終于找到答案,記錄一下。

  • 我們用 mongoose 作為連接庫(kù)
  • 聯(lián)查需要用?$lookup
  • 如果聲明外鍵的時(shí)候用?ObjectId,就很簡(jiǎn)單:
// 假設(shè)下面兩個(gè)表 db1 和 db2
export const Db1Schema = new mongoose.Schema(
  {
    userId: { type: String, index: true },
    couponId: { type: ObjectId, ref: Db2Schema },
  },
  { versionKey: false, timestamps: true }
);
export const Db2Schema = new mongoose.Schema(
  {
    status: { type: Boolean, default: 0 },
  },
  { versionKey: false, timestamps: true }
);

// 那么只要
db1Model.aggregate([
  {
    $lookup: {
      from: 'db2', // 目標(biāo)表
      localField: 'couponId', // 本地字段
      foreignField: '_id', // 對(duì)應(yīng)的目標(biāo)字段
      as: 'source',
  },
  {
    $match: [ /* 各種條件 */ ],
  },
]);

但是我們沒(méi)有用?ObjectId,而是用?string?作為外鍵,所以無(wú)法直接用上面的聯(lián)查。必須在?pipeline?里手動(dòng)轉(zhuǎn)換、聯(lián)合。此時(shí),當(dāng)前表(db1)的字段不能直接使用,要配合?let,然后加上?$$?前綴;連表(db2)直接加?$?前綴即可。

最終代碼如下:

mongo.ts

// 每次必有的條件,當(dāng)前表的字段用 `$$`,連表的字段用 `$`
const filter = [{ $eq: ['$$userId', userId] }, { $eq: ['$isDeleted', false] }];
if (status === Expired) {
  dateOp = '$lte';
} else if (status === Normal) {
  dateOp = '$gte';
  filter.push({ $in: ['$$status', [Normal, Shared]] });
} else {
  dateOp = '$gte';
  filter.push({ $eq: ['$$status', status] });
}
const results = await myModel.aggregate([
  {
    $lookup: {
      from: 'coupons',
      // 當(dāng)前表字段必須 `let` 之后才能用
      let: { couponId: '$couponId', userId: '$userId', status: '$status' },
      // 在 pipeline 里完成篩選
      pipeline: [
        {
          $match: {
            $expr: {
              // `$toString` 是內(nèi)建方法,可以把 `ObjectId` 轉(zhuǎn)換成 `string`
              $and: [{ $eq: [{ $toString: '$_id' }, '$$couponId'] }, ...filter, { [dateOp]: ['$endAt', new Date()] }],
            },
          },
        },
        // 只要某些字段,在這里篩選
        {
          $project: couponFields,
        },
      ],
      as: 'source',
    },
  },
  {
    // 這種篩選相當(dāng) LEFT JOIN,所以需要去掉沒(méi)有連表內(nèi)容的結(jié)果
    $match: {
      source: { $ne: [] },
    },
  },
  {
    // 為了一次查表出結(jié)果,要轉(zhuǎn)換一下輸出格式
    $facet: {
      results: [{ $skip: size * (page - 1) }, { $limit: size }],
      count: [
        {
          $count: 'count',
        },
      ],
    },
  },
]);

同事告訴我,這樣做的效率不一定高。我覺(jué)得,考慮到實(shí)際場(chǎng)景,他說(shuō)的可能沒(méi)錯(cuò),不過(guò),早晚要邁出這樣的一步。而且,未來(lái)我們也應(yīng)該慢慢把外鍵改成?ObjectId?類(lèi)型。

總結(jié)

原文鏈接:https://blog.meathill.com/js/nodejs/mongodb-mongoose-support-left-join-filter.html

欄目分類(lèi)
最近更新