MongoDB - 如何根据部分字符串查找不同的值 [英] MongoDB - How to find distinct values based on partial string

查看:58
本文介绍了MongoDB - 如何根据部分字符串查找不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可行,但我想以 Mongo 的不同方法从 url 中获取不同的域名.以下是一些示例数据:

I'm not sure if this is even possible, but I'd like to get distinct domain names from urls in a Mongo distinct method. Here is some sample data:

     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "http://mydomain.prep.com/post/290837872/myContent"
     }
     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "http://mydomain.prep.com/s/44432/somethingElse"
     }
     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "https://newdomain.com/ref/2"
     }
     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "http://olddomain.reference.org/ref/5"
     }
     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "https://newdomain.com/ref/2342"
     }
     {
      stuff : "someValue",
      moreStuff : "someOtherValue",
      url : "http://olddomain.reference.org/ref/1234"
     }

因此,基于示例数据,我想获得一个不同的查询,该查询仅返回网址中的不同域:

So based on the sample data, I'd like to get to have a distinct query that simply returns the distinct domains within the urls:

    {
     "0" : "http://mydomain.prep.com",
     "1" : "https://newdomain.com",
     "2" : "http://olddomain.reference.org"
    }

有人可以告诉我如何做这个查询吗?我对 Mongo 查询很陌生,还没有在网上找到解决方案.谢谢!

Could someone please show me how to do this query? I'm pretty new with Mongo queries and have not found a solution online. Thanks!

推荐答案

从 MongoDB 2.6.3 开始,执行此类操作的字符串处理功能不可用.您可以通过 map/reduce 计算不同的域名,您必须编写自己的逻辑来从 url 确定域名,但我认为更简单的事情是在客户端计算域名并将其插入文件:

As of MongoDB 2.6.3, the string processing capabilities to do something like this aren't available. You could compute distinct domain names via map/reduce, where you'd have to write your own logic for determining a domain name from the url, but I think the easier thing is to compute the domain name on the client side and insert it in the document:

{
  stuff : "someValue",
  moreStuff : "someOtherValue",
  url : "http://mydomain.prep.com/post/290837872/myContent"
  domain : "mydomain.prep.com"
 }

然后你可以使用一个简单的不同查询

Then you can use a simple distinct query

db.urls.distinct("domain")

这篇关于MongoDB - 如何根据部分字符串查找不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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