Mongodb Aggregation:如何仅返回数组的匹配元素 [英] Mongodb Aggregation : How to return only matching elements of an array

查看:575
本文介绍了Mongodb Aggregation:如何仅返回数组的匹配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的mongoDB图书集中,我的文档结构如下:

In my mongoDB book collection I have documents structured as follow :

/* 0 */
{
  "_id" : ObjectId("50485b89b30f1ea69110ff4c"),

  "publisher" : {
    "$ref" : "boohya",
    "$id" : "foo"
  },
  "displayName" : "Paris Nightlife",
  "catalogDescription" : "Some desc goes here",
  "languageCode" : "en",
  "rating" : 0,
  "status" : "LIVE",
  "thumbnailId" : ObjectId("50485b89b30f1ea69110ff4b"),
  "indexTokens" : ["Nightlife", "Paris"]
}

我执行以下正则表达式查询,以查找所有具有一个以"Par"开头的indexToken的文档:

I perform the following regex query to find all documents having one indexToken starting with "Par" :

{ "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}}

如果我仅选择要返回的indexTokens字段,如下所示:

If I select only the indexTokens field to be returned like this :

{ "indexTokens" : 1}

生成的DBObject是

The resulting DBObject is

{ "_id" : { "$oid" : "50485b89b30f1ea69110ff4c"} , "indexTokens" : [ "Nightlife" , "Paris"]}

我只想获得与正则表达式匹配的令牌/标记(我现在不关心检索文档,我也不需要匹配的文档的所有标记)

What I would like to get is ONLY the token / tag that matched the regex (I don0t care about retrieving the document at this point, neither do I need all the tags of the matched document)

在MongoDB v2.2下发布的新聚合框架是否属于这种情况? ?

Is this a case for the new Aggregation Framework relesed under MongoDB v2.2. ?

如果是,我如何修改查询,以便实际结果如下所示:

If yes how do I modify my query so that the actual result would look like :

{"indexTokens":[巴黎",天堂河",帕尔马"等....]}

{ "indexTokens" : ["Paris", "Paradise River", "Parma" , etc ....]}

奖金问题(您是否有codez):如何使用Java驱动程序来做到这一点?

Bonus question (do you has teh codez) : How do I do it using the Java driver ?

现在我的java看起来像:

For now my java looks like :

DBObject query = new BasicDBObject("indexTokens", java.util.regex.Pattern.compile("^"+filter+"", Pattern.CASE_INSENSITIVE));
    BasicDBObject fields = new BasicDBObject("indexTokens",1);
    DBCursor curs = getCollection()
                    .find(query, fields)
                    .sort( new BasicDBObject( "indexTokens" , 1 ))
                    .limit(maxSuggestionCount);

Thx :)

根据您的回答,我将我的JAVA代码修改如下:

As per your answers I modified my JAVA code as follow :

BasicDBObject cmdBody = new BasicDBObject("aggregate", "Book"); 
    ArrayList<BasicDBObject> pipeline = new ArrayList<BasicDBObject>(); 

    BasicDBObject match = new BasicDBObject("$match", new BasicDBObject("indexTokens", java.util.regex.Pattern.compile("^"+titleFilter+"", Pattern.CASE_INSENSITIVE)));
    BasicDBObject unwind = new BasicDBObject("$unwind", "$indexTokens");
    BasicDBObject match2 = new BasicDBObject("$match", new BasicDBObject("indexTokens", java.util.regex.Pattern.compile("^"+titleFilter+"", Pattern.CASE_INSENSITIVE)));
    BasicDBObject groupFilters = new BasicDBObject("_id",null);
    groupFilters.append("indexTokens", new BasicDBObject( "$push", "$indexTokens"));
    BasicDBObject group = new BasicDBObject("$group", groupFilters);

    pipeline.add(match);
    pipeline.add(unwind);
    pipeline.add(match2);
    pipeline.add(group);

    cmdBody.put("pipeline", pipeline); 



    CommandResult res = getCollection().getDB().command(cmdBody);
    System.out.println(res);

哪个输出

{ "result" : [ { "_id" :  null  , "indexTokens" : [ "Paris"]}] , "ok" : 1.0}

这是天才!

非常感谢!

推荐答案

您可以使用2.2聚合框架来做到这一点.像这样的东西

You could do this with the 2.2 aggregation framework. Something like this;

db.books.runCommand("aggregate", {
    pipeline: [
        {   // find docs that contain Par*
            $match: { "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}},
        },
        {   // create a doc with a single array elemm for each indexToken entry
            $unwind: "$indexTokens" 
        },
        {   // now produce a list of index tokens
            $group: {
                _id: "$indexTokens",
            },
        },
    ],
})

或者,如果您真的想要没有文档的数组,则可能更接近您想要的内容;

Or this might be even closer to what you're after if you really want the array without the doc;

db.books.runCommand("aggregate", {
    pipeline: [
        {   // find docs that contain Par*
            $match: { "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}},
        },
        {   // create a doc with a single array elemm for each indexToken entry
            $unwind: "$indexTokens" 
        },
        {   // now throw out any unwind's that DON'T contain Par*
            $match: { "indexTokens": { "$regex": "^Par", "$options": "i" } },
        },
        {   // now produce the list of index tokens
            $group: {
                _id: null,
                indexTokens: { $push: "$indexTokens" },
            },
        },
    ],
})

这篇关于Mongodb Aggregation:如何仅返回数组的匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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