How to modify Mongo values in where-clause in query?

我们两清 提交于 2019-12-13 03:14:27

问题


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

This is the one I use right now:

$db->users->findOne(array("redirect_uri" => $arrParsedUrl['host']));
  • In this case I need to make sure that the value earlier inserted into the database is only the host part of the 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']));
  • In this case I would be able to insert the complete URL in the database from the beginning. The "parse_url" function in the query makes sure that only the host part is actually compared.

Any ideas how to achive something similar?

Edit: 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.

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

Edit2:

  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?

回答1:


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

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

or findAndModify (this one updates and retrieves)

Edit:

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

Edit2:

  • You can use regular expressions.
  • You can restructure your database to store the host in a separate field.


来源:https://stackoverflow.com/questions/6923962/how-to-modify-mongo-values-in-where-clause-in-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!