Reputation: 4630
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
Upvotes: 0
Views: 43
Reputation: 13081
You need to escape the regex around the braces [ ].
db.creative.aggregate([ { $match : { tag : { $regex : /\[FAST_1\]/ } }}, ...
Upvotes: 1