【问题标题】:Can I use $match to match a data based on whether it empty or not In MongoDb我可以使用 $match 根据数据是否为空在 MongoDb 中匹配数据吗
【发布时间】:2021-03-21 14:20:34
【问题描述】:

就像我的情况一样,我想根据过滤器的值匹配过滤器。如果过滤器值为空,我想跳过该字段进行匹配。这可能在同一个查询中吗?

我可以根据它是否为空来$match 数据。在 MongoDB 中

出现以下错误:

ERROR - Cast to boolean failed for value "" at path "undefined"
      at castBoolean (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\cast\boolean.js:28:9)
      at SchemaBoolean.cast (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\schema\boolean.js:195:12)
      at SchemaBoolean.SchemaType.applySetters (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\schematype.js:1075:12)
      at SchemaBoolean.SchemaType._castForQuery (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\schematype.js:1510:15)
      at SchemaBoolean.castForQuery (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\schema\boolean.js:224:15)
      at SchemaBoolean.SchemaType.castForQueryWrapper (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\schematype.js:1477:20)
      at cast (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\cast.js:331:32)
      at cast (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\cast.js:74:18)
      at model.Query.Query.cast (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\query.js:4772:12)
      at model.Query.<anonymous> (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\query.js:2267:10)
      at model.Query._wrappedThunk [as _countDocuments] (D:\js\Anoko\anoko-backend\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)
      at D:\js\Anoko\anoko-backend\node_modules\kareem\index.js:369:33
      at processTicksAndRejections (internal/process/task_queues.js:79:11) {
    stringValue: '""',
    messageFormat: undefined,
    kind: 'boolean',
    value: '',
    path: undefined,
    reason: undefined
  }

代码:

const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit);

const startIndex = (page - 1) * limit;
const endIndex = page * limit;
const q = req.query.q;

const filter = req.query.filter === "true" ? true : !req.query.filter ? "" : false;
console.log("Search query", filter);

User.aggregate([{
    $match: {
        $and: [
            {
                $or: [
                    { email: { $regex: new RegExp("^" + q.toLowerCase(), "i") } },
                    { firstName: { $regex: new RegExp("^" + q.toLowerCase(), "i") } },
                ],
            },
            {
                block: !filter ? null : filter,
            },
        ],
    },
}, {
    $lookup: {
        from: "userdetails",
        localField: "_id",
        foreignField: "user",
        as: "details",
    },
}, {
    $lookup: {
        from: "userbios",
        localField: "_id",
        foreignField: "user",
        as: "bios",
    },
}, {
    $lookup: {
        from: "usersettings",
        let: { userID: "$_id" },
        pipeline: [
            {
                $match: {
                    $expr: {
                        $and: [
                            {
                                $eq: ["$user", "$$userID"],
                            },
                        ],
                    },
                },
            },
            { $project: { finalConnection: 1, matches: 1 } },
        ],
        as: "userInfo",
    },
}, { $project: { password: 0, _v: 0, createdAt: 0 } },
])
    .skip(startIndex)
    .limit(limit)

【问题讨论】:

    标签: node.js regex mongodb express mongoose


    【解决方案1】:

    在查询之前创建你的 where 条件,

    where = {};
    

    根据您的要求创建类似这样的检查,

      if (filter) {
        where.block = filter
      }
    

    排除空检查并通过唯一必要的 where 条件来匹配阶段。这样可以避免查询中出现一些多余的匹配项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多