field_name 中的 Mongoose 过滤器对象

问题描述 投票:0回答:3

enter image description here

我想按用户名列出汽车,我该怎么做?

mongoose mongoose-schema
3个回答
0
投票

enter image description here

使用您建议的方法,响应返回空。

-来自以下代码的响应

enter image description here


0
投票

要从另一个文档中过滤文档,您需要使用填充字段。

为此,我们使用聚合。在查找中;我们将 cars 字段与 user 字段连接起来,以达到用户字段的值,如用户名等。

在 MongoDB Compass 中,您需要使用页面左下角的

mongosh
。首先, 需要实现您想要进行过滤的数据库。为此;

use arpaslanOto

现在,您切换了数据库。当前分贝是

alparslanOto
。然后在字段中写下您要搜索的过滤器;

db.cars.aggregate([
          {
            $lookup: {
              from: "users",
              localField: "user",
              foreignField: "_id",
              as: "UserCarTable",
            },
          },
          {
            $match: { "UserCarTable.username": username },
          },
]);

希望对你有用


-1
投票
 db.cars.aggregate([
        {
          $lookup: {
            from: "users",
            localField: "user",
            foreignField: "_id",
            as: "cars",
          },
        },
        {
          $match: { "cars.name": "Mustafa" },
        },
      ])

工作!!

© www.soinside.com 2019 - 2024. All rights reserved.