MongoDB $lookup返回空数组

vom3gejh  于 2023-04-20  发布在  Go
关注(0)|答案(2)|浏览(185)

产品系列:

[
        {
            "isActive": true,
            "_id": "643bbdee68b521035e20c976",
            "name": "lu",
            "price": 123,
            "userId": "643abd0dcce4df3b1a0c8fa7"
        },
         {
            "_id": "643bbdd668b521035e20c972",
            "name": "mu",
            "price": 123,
            "userId": "643abd0dcce4df3b1a0c8fa7",
            "isActive": true,
            "__v": 0
        },
         {
            "isActive": true,
            "_id": "643bbdbc68b521035e20c971",
            "name": "nu",
            "price": 123,
            "userId": "643abd0dcce4df3b1a0c8fa7"
        },
         {
            "_id": "643c2dc126211992b4ee8fd5",
            "name": "pu",
            "price": 123,
            "userId": "643abd0dcce4df3b1a0c8fa7",
            "isActive": true
        }
    ]

子产品采集:

[
        {
            "_id": "643d7ed82cd5b5fcd7e81e02",
            "name": "kkkk",
            "phoneNum": 9543922499,
            "productID": "643bbdee68b521035e20c976",
            "user": "643abd0dcce4df3b1a0c8fa7",
            "address": {
                "address1": "243, bastin nagarsss",
                "zipCode": 6250185
            }
        }
    ]

我的聚合函数:

aggregateLookup: async () => {
    try {
        let subProduct = await SubProduct.aggregate([
            {
                $lookup: {
                    from: "Product",
                    localField: "productID",
                    foreignField: "_id",
                    as: "product_Details"
                }
            }
        ]);

        return subProduct;
    } catch (error) {
        throw new Error(error);
    }
}

我的方案:

const subProduct = new mongoose.Schema({
  name: {
    type: String,
    required: [true,'Name is Required...']
  },
  phoneNum: {
    type: Number,
    required: true,
    validate: {
      validator: (value) => {
        return `${value}`.length === 10;
      },
      message: 'Should be 10 digit Number'
    }
  },
  productID: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'Product'
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'User'
  },
  address: {
    address1: String,
    zipCode: Number
  }
});

在Schema中,我将Productid类型设置为ObjectID。在aggregateLookup函数中,我需要使用$lookup检索所有数据,我将相关产品保存在ProductDocument中,但它仍然返回空数组。
我的输出中Product_Details为空,但在Product Documents中有数据:

[
    {
        "_id": "643d7ed82cd5b5fcd7e81e02",
        "name": "kkkk",
        "phoneNum": 9543922499,
        "productID": "643bbdee68b521035e20c976",
        "user": "643abd0dcce4df3b1a0c8fa7",
        "address": {
            "address1": "243, bastin nagarsss",
            "zipCode": 6250185
        },
        "product_Details": []
    }
]

预期输出:

[
    {
        "_id": "643d7ed82cd5b5fcd7e81e02",
        "name": "kkkk",
        "phoneNum": 9543922499,
        "productID": "643bbdee68b521035e20c976",
        "user": "643abd0dcce4df3b1a0c8fa7",
        "address": {
            "address1": "243, bastin nagarsss",
            "zipCode": 6250185
        },
        "product_Details": [
        {
        "isActive": true,
        "_id": "643bbdee68b521035e20c976",
        "name": "lu",
        "price": 123,
        "userId": "643abd0dcce4df3b1a0c8fa7"
        }
        ]
    }
]

请帮助我我犯了什么错误我是新的mongodb.提前感谢...

z4iuyo4d

z4iuyo4d1#

问题当然是集合的名称。
你已经使用了from:"Product",你需要使用单词products,因为你需要提供在数据库中形成的集合的确切名称。因为mongoDB会自动为集合名称分配复数单词。你可以通过转到mongosh shell和
1.显示所有数据库show dbs的列表
1.使用数据库名称use db_name
1.列出集合show collections
https://mongoplayground.net/p/_bp4jpzUHVg
我在这里测试过了

o7jaxewo

o7jaxewo2#

@Rituparan我检查了一些文档,我准备了下面的查询,它没有按预期工作,它的子项目的coming dirrect子项目具有空数组。

await SubProduct.aggregate([
                {
                    $lookup: {
                        from: "products",
                        localField: "productID",
                        foreignField: "_id",
                        as: "product_Details"
                    }
                },
                {
                    $unwind: "$product_Details"
                },
                {
                    $lookup: {
                        from: "users",
                        localField: "product_Details.userId",
                        foreignField: "_id",
                        as: "user_Details"
                    }
                },{
                    $project: {
                        "user_Details.password": 0
                    }
                }
            ]);

但我的输出是

[
    {
        "_id": "643d7ed82cd5b5fcd7e81e02",
        "name": "kkkk",
        "phoneNum": 9543922499,
        "productID": "643bbdee68b521035e20c976",
        "user": "643abd0dcce4df3b1a0c8fa7",
        "address": {
            "address1": "243, bastin nagarsss",
            "zipCode": 6250185
        },
        "product_Details": {
            "_id": "643bbdee68b521035e20c976",
            "name": "lu",
            "price": "123",
            "userId": "643abd0dcce4df3b1a0c8fa7"
        },
        "user_Details": []
    }
]

我喜欢在Product_Details下有user_Details的值,所以在查询$lookup中从“user”添加localField作为“Product_Details.userId”仍然不起作用。我犯了什么错误。请让我知道。我使用MongoDB版本5.0。
预期输出:

[
  {
    "_id": "643d7ed82cd5b5fcd7e81e02",
    "address": {
      "address1": "243, bastin nagarsss",
      "zipCode": 6.250185e+06
    },
    "name": "kkkk",
    "phoneNum": 9.543922499e+09,
    "productID": "643bbdee68b521035e20c976",
    "product_Details": [
      {
        "_id": "643bbdee68b521035e20c976",
        "isActive": true,
        "name": "lu",
        "price": 123,
        "userId": "643abd0dcce4df3b1a0c8fa7",
        "user_Details": [
          {
            "_id": "643abd0dcce4df3b1a0c8fa7",
            "age": 23,
            "name": "Rituparna Warwatkar"
          }
        ]
      }
    ],
    "user": "643abd0dcce4df3b1a0c8fa7"
  }
]

相关问题