如何在查询中的 where 子句中修改 Mongo 值? [英] How to modify Mongo values in where-clause in query?

查看:44
本文介绍了如何在查询中的 where 子句中修改 Mongo 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以修改查找查询中 where 子句中的 mongo 值?

Is there any way to modify the mongo values in a where-clause in a find query?

这是我现在使用的:

$db->users->findOne(array("redirect_uri" => $arrParsedUrl['host']));

  • 在这种情况下,我需要确保之前插入到数据库中的值只是 URL 的主机部分.
  • 我想做的是这样的事情(但它不起作用):

    What I would like to do is someting like this (but it's not working):

    $db->users->findOne(array(parse_url("redirect_uri")['host'] => $arrParsedUrl['host']));
    

    • 在这种情况下,我可以从一开始就在数据库中插入完整的 URL.查询中的parse_url"函数确保实际只比较主机部分.
    • 任何想法如何实现类似的东西?

      Any ideas how to achive something similar?

      我不想更新数据库中的值.仅修改 where 子句中使用的数据库中与输入值进行比较的值.

      I don't want to update the values in the database. Only modify the value from the database used in the where-clause that is compared to the input value.

      输入值将始终是 URL 的主机部分.

      The input value will always be the host part of an URL.

      编辑 2:

      1. 我在名为 users 的集合中有多个文档.
      2. 所有文档都有一个名为redirect_uri"的字段,其中包含一个 URL.
      3. 当我使用 url 作为输入调用 php 函数时,我想返回输入 URL 的主机部分与文档中redirect_uri"的主机部分相同的文档.例如.http://www.foo.com/bananas ("redirect_url") = http://www.foo.com/apples(输入网址)
      4. 如何查询 MongoDB 以获取redirect_url"的主机部分与输入 URL 的主机部分相同的文档?将输入 URL 修改为 www.foo.com 不是问题,但如何以相同的方式修改redirect_url"部分以使查询给出正确答案?
      1. I have several documents in a collection called users.
      2. All documents have a field called "redirect_uri" that contains an URL.
      3. When I call a php function with an url as input I would like to return the document where the host part of the input URL is the same as the host part of the "redirect_uri" in the document. E.g. http://www.foo.com/bananas ("redirect_url") = http://www.foo.com/apples (input URL)
      4. How can I query MongoDB to get the document where the host part of the "redirect_url" is the same as the host part of the input URL? To modify the input URL to www.foo.com is not a problem but how do I modify the "redirect_url" part in the same way to make the query give me a correct answer?

      推荐答案

      你的问题有点含糊,你要么在找 更新(这个只是更新):

      Your question is a bit ambiguous, you are either looking for update (this one just updates):

      db.collection.update( criteria, objNew, upsert, multi );
      

      findAndModify(这个更新和检索)

      只需在客户端进行转换即可.mongoDB 的理念是将尽可能多的工作推送给客户.e

      Just do the transformation on the client side. The mongoDB philosophy is to push as much work to the clients as possible.e

      编辑 2:

      • 您可以使用正则表达式.
      • 您可以重构数据库以将主机存储在单独的字段中.

      这篇关于如何在查询中的 where 子句中修改 Mongo 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆