MongoDB:查询子文档:Java

时间:2014-12-16 23:11:44

标签: java regex mongodb mapreduce aggregation-framework

编写mongoDB'查询并更新'在Java。

mongoDB Collection(Name:reduced)结果(map-reduce result)如下所示:

值字段:

{ "value" : 

    { 
        "User_Name" : "Mitchamoreagent",
        "tweets" : ["RT    Perspectives:
        Texas Insurer of Last Resort Charts Course Through Reform Law", "RT  Texas sale
        s tax-free weekend set for Aug. 19-21", "RT  The New Normal: Billion-Dollar", "R
        T  Austin Water is responding to a 12-inch water main leak at Burnet Rd. and And
        erson Lane in North Austin."] 
    }
}

尝试找到其推文中包含“'”字样的所有User_Name。我可以使用正则表达式指定。

为了实现这一点,我尝试了AggregationOutput,它可以在简单的情况下正常工作。但是这种结构,我无法通过它。

代码:

 DBObject match = new BasicDBObject("$match",new BasicDBObject("value",new    BasicDBObject("tweets", new BasicDBObject("$regex",".*Texas.*"))));
 DBObject fields = new BasicDBObject("_id", 0); 
 DBObject nest = new BasicDBObject("value", new BasicDBObject("User_ID", 1));
 fields.put("value", 1);
 DBObject project = new BasicDBObject("$project", fields );
 AggregationOutput output = tweets.aggregate( match, project);

这里' Texas'是我想要找到的单词,我希望输出[Mitchamoreagent].

但是输出总是一个例外noRowsReturned。

1 个答案:

答案 0 :(得分:2)

您需要构建$match阶段操作,如下所示:

 DBObject regex = new BasicDBObject("$regex","Texas");
 DBObject condition = new BasicDBObject("value.tweets",regex);
 DBObject match = new BasicDBObject("$match",condition);

将在您的问题中生成的相应mongodb查询将是:

{$match:{"value":{"tweets":{$regex:"Texas"}}}}

实际应该形成为:

{$match:{"value.tweets":{$regex:"Texas"}}}

项目阶段应按如下方式构建:

如果需要将嵌套字段的值作为顶级字段投影,在本例中为User_name,则需要使用别名进行投影,在这种情况下,别名为user_name

 DBObject fields = new BasicDBObject("_id", 0); 
 fields.put("user_name", "$value.User_Name"); // project the user_name for each record.

执行管道:

 DBObject project = new BasicDBObject("$project", fields );
 AggregationOutput output = tweets.aggregate( match, project);