Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB下查询中包含数组/对象时自动加 $in 的问题。 #513

Open
CNBroderick opened this issue Dec 26, 2023 · 1 comment
Open

Comments

@CNBroderick
Copy link

使用版本:

{ 
  "topthink/think-orm": "^3.0"
}

争议代码位置:
src/db/concern/WhereQuery.php:parseArrayWhereItems#504

当以此代码作为查询条件时:

$where = [
    'time' => [
        '$gte' => 1703508340 * 1000000000,
        '$lt' => (1703508340 + 1) * 1000000000,
    ],
    'ip' => [
        '$in' => [
            '123.123.123.123',
            '1.1.1.1',
            '2.2.2.2',
        ]
    ]
];
(new UserLoginLog())->where($where)->field('_id')->failException()->find();

实际的查询条件为:

db.user_login_log
  .find({
    $and: [
      {
        time: {
          $in: { $gte: 1703508340000000000, $lt: 1703508341000000000 },
        },
      },
      {
        time: {
          $in: { $in: ["123.123.123.123", "1.1.1.1", "2.2.2.2"] },
        },
      },
    ],
  })
  .limit(1);

咨询:
是否有一个方法,可以像ThinkPHP 3.2.3时的MongoModel中的写法,将以上的查询条件直接进行 json_encode操作呢?

@CNBroderick
Copy link
Author

当前找到的解决方法是将array强转成object,这个办法是否是推荐写法?如果是可以加到文档中吗?

$where = [
    'time' => (object) [
        '$gte' => 1703508340 * 1000000000,
        '$lt' => (1703508340 + 1) * 1000000000,
    ],
    'ip' => [
        '$in' => (object) [
            '123.123.123.123',
            '1.1.1.1',
            '2.2.2.2',
        ]
    ]
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant