简体   繁体   English

在聚合查询中使用$ match

[英]Using $match in aggregation query

I have these values in my Mongodb: 我的Mongodb中有这些值:

{
"_id" : ObjectId("4feb9d573752c8a33a000001"),
"name" : "TSP1",
"Server" : "S1",
"active" : true,
"tag" : "<A HREF=\"[FAST_1]http://www.google.com"><IMG SRC=\"http://ad.google.com.CO/B5981883.7;sz=300x50;ord=[TIMESTAMP]?\" BORDER=0 WIDTH=300 HEIGHT=50 ALT=\"ment\"></A>"
},
{
"_id" : ObjectId("4feb9d573752c8a33a000001"),
"name" : "TSP2",
"Server" : "S1",
"active" : true,
"tag" : "<A HREF=\"[FAST_1]http://www.google.com"><IMG SRC=\"http://ad.ITG.com.CO/B5981883.7;sz=300x50;ord=[TIMESTAMP]?\" BORDER=0 WIDTH=300 HEIGHT=50 ALT=\"ment\"></A>"
}
{
"_id" : ObjectId("4feb9d573752c8a33a000003"),
"name" : "TSP3",
"Server" : "S2",
"active" : true,
"tag" : "<A HREF=\"[FAST_2]http://www.google.com"><IMG SRC=\"http://ad.Yahoo.com.CO/B5981883.7;sz=300x50;ord=[TIMESTAMP]?\" BORDER=0 WIDTH=300 HEIGHT=50 ALT=\"ment\"></A>"
}

I am trying to get this result out: 我试图得到这个结果:

    "result" : [
    {
        "_id" : "S1",
        "count" : 2
    }]

This is the query that i am using: 这是我正在使用的查询:

db.creative.aggregate([ { $match : { tag : { $regex : /[FAST_1]/ } }}, { $group: { _id : "$Server", count: { $sum: 1} }} ]);

Also, tried this: 另外,尝试了这个:

db.creative.aggregate([ {$match : {"tag" : /[FAST_1]/ }}, { $group: { _id : "$Server", count: { $sum: 1}}} ])

But i keep getting this result: 但我一直得到这个结果:

    {
        "_id" : "S1",
        "count" : 2
    },
    {
        "_id" : "S2",
        "count" : 1
    }

Even if i change FAST_1 to FAST_2 i get the same result. 即使我将FAST_1更改为FAST_2,我也会得到相同的结果。

Any help is appreciated. 任何帮助表示赞赏。

Thanks 谢谢

您需要在大括号[]周围使用正则表达式。

db.creative.aggregate([ { $match : { tag : { $regex : /\[FAST_1\]/ } }},  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM