ParisNakitaKejser
ParisNakitaKejser

Reputation: 14959

MongoDB $match and $group problems

i trying to get a match count out becures i have a kind of problems so i will change my find().count() to aggregate() methode.

My problem is when i add my $match its not return nothing in count, if i remove the line its works fine.

db.getCollection('product').aggregate(
   [
      { $match: { 'Store.Title': { $regex: '/apc/', $options: 'gi' } } },
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
)

can somebody help me and explain what i do wrong here?

Upvotes: 1

Views: 52

Answers (1)

chridam
chridam

Reputation: 103475

You can try using the RegExp object to create the regex object first then use it in your pipeline:

var apc_match = new RegExp('apc', 'gi');

db.getCollection('product').aggregate(
   [
      { $match: { 'Store.Title': apc_match } },
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
);

OR

db.getCollection('product').aggregate(
   [
      { $match: { 'Store.Title': { $regex: 'apc', $options: 'gi' } } },
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
);

OR

db.getCollection('product').aggregate(
   [
      { $match: { 'Store.Title': /apc/gi } },
      { $group: { _id: null, count: { $sum: 1 } } }
   ]
);

Upvotes: 1

Related Questions