【问题标题】:Mongoose, aggregate query with $lookup return null?Mongoose,使用 $lookup 的聚合查询返回 null?
【发布时间】:2020-05-19 02:42:48
【问题描述】:

variantalt 模型:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const varyantaltSchema = new Schema({
  _id: {
    type: Schema.ObjectId
  },
  varyantid: {
    type: String
  },
  altvaryantname: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model("varyantalt", varyantaltSchema, "varyantalt");

变种型号:

const varyantSchema = new Schema({
  _id: {
    type: Schema.ObjectId
  },
  stokid: {
    type: Schema.ObjectId
  },
  varyantname: {
    type: String
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model("varyant", varyantSchema, "varyant");

变体集合:

{
    "_id": ObjectId("5e32286c34fb7322bdd566ed"),
    "stokid": ObjectId("5e28b4a2a1d9692b29a65b24"),
    "varyantname": "RENK"
}

altvariant 集合:

{
    "_id": ObjectId("5e3228df34fb7322bdd566f5"),
    "varyantid": "5e32286c34fb7322bdd566ed",
    "altvaryantname": "KIRMIZI",
    "vars": [
        {
            "_id": ObjectId("5e35b1e410fce83f3370cd0a"),
            "images": {
                "imageurl": "http://",
                "_id": ObjectId("5e35b1e410fce83f3370cd0b"),
                "filename": "5320_7d93",
                "path": "https://res",
                "publicid": "panel"
            }
        }
    ]
},
{
    "_id": ObjectId("5e3359e6fa4c5e4bd9112fb6"),
    "varyantid": "5e32286c34fb7322bdd566ed",
    "altvaryantname": "SARI",
    "vars": [
        {
            "_id": ObjectId("5e35b1f610fce83f3370cd0d"),
            "images": {
                "imageurl": "http://",
                "_id": ObjectId("5e35b1f610fce83f3370cd0e"),
                "filename": "veli-fidan-1LT-2-450x450",
                "path": "https://",
                "publicid": "panel"
            }
        }
    ]
},
{
    "_id": ObjectId("5e335b64fa4c5e4bd9112fc9"),
    "varyantid": "5e32286c34fb7322bdd566ed",
    "altvaryantname": "YEŞİL",
    "vars": [
        {
            "_id": ObjectId("5e35b20010fce83f3370cd10"),
            "images": {
                "imageurl": "http://",
                "_id": ObjectId("5e35b20010fce83f3370cd11"),
                "filename": "maxresdefault-29-450x450",
                "path": "https://",
                "publicid": ""
            }
        }
    ]
}

查询:

varyant
  .aggregate([
    {
      $match: {
        stokid: mongoose.Types.ObjectId("5e28b4a2a1d9692b29a65b24")
      }
    },
    {
      $lookup: {
        from: "varyantalt",
        localField: "_id",
        foreignField: "varyantid",
        as: "vars"
      }
    },
    { $unwind: "$vars" }
  ])
  .exec((err, locations) => {
    if (err) throw err;
    console.log("res::", locations);
  });

我正在尝试使用 $aggregate$lookup 使用 mongoose 查询结果 为什么我的查询返回空?我找不到问题。

$lookup 下操作符from: 'varyantalt' 不明白到底是不是取空值。提前感谢您的帮助。

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    在您的 varyantaltSchema 架构中,varyantid 被定义为字符串,但它必须是 Schema.ObjectId

    它们必须相同,因为$lookup 对输入文档中的foreignField 与localField 执行相等匹配

    如果您将varandid 保存为对象ID 聚合将起作用。

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 2021-10-25
      • 2017-02-16
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多