Mongoose,使用 $lookup 的聚合查询返回 null?

Posted

技术标签:

【中文标题】Mongoose,使用 $lookup 的聚合查询返回 null?【英文标题】:Mongoose, aggregate query with $lookup return 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' 不明白到底是不是取空值。提前感谢您的帮助。

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

以上是关于Mongoose,使用 $lookup 的聚合查询返回 null?的主要内容,如果未能解决你的问题,请参考以下文章

Mongoose $lookup 聚合没有按预期工作

来自多个集合的 $lookup 的 Mongoose 聚合返回空集

Mongoose.aggregate(pipeline) 使用 $unwind、$lookup、$group 链接多个集合

MongoDB 聚合 - $lookup 性能

MongoDB mongoose $lookup 不在数组中。

Mongoose 聚合查找 - 如何按特定 id 过滤