简体   繁体   中英

Using $match in aggregation query

I have these values in my 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.

Any help is appreciated.

Thanks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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