Not able to use $group with $search in atlas search

Using the below query to run in mongo atlas.

db.inventory.aggregate([
  {
    $search: {
	"text": {
        "query": "..",
        "path": [field_names]
      }
    }
  }, 
  {
$match:{condition}
},
  {
      $group:
    }
]);

There is no error. But there is no output as well. if $group is remove from the command, there is an output. Kindly let me know if there is other options to do grouping.

Hi @Durga_Krishnamoorthi,

Please provide more details like full query and some sample documents

Best
Pavel

_id,productID,vendorID,category,subCategory,productName,productDesc,productImage,unitPrice,unitPriceCurrency,shippingCharge,stockQty,availableQty,estimatedDelivery,cashOnDelivery,createdBy,createdDt,lastUpdBy,lastUpdDt,expanded
5f48dc593ed75a76be5ae30b,PR-HEAB5,VD-AB00SM01,JBL Headset,Wired headset W201,JBL Headset Wired headset W201 Sold by VD-AB00SM01,Wired Headset - White Sold by VD-AB00SM01,false,1200,INR,5,10,10,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae30c,PR-HEAB3,VD-AB00SM01,JBL Headset,Bluetooth Headset C101,JBL Headset Bluetooth Headset C101 sold by  Sold by VD-AB00SM01,"Leo JBL° C100si Wired Headset without Mic  (Red, Wired in the ear) Sold by VD-AB00SM010",http://192.168.2.94:8080/reference/api/v1.0/gfs/import/?filename=PR-HEAB3_VD-AB00SM01_1:1,1000,INR,5.01,300,294,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,admin,2020-08-26T14:09:16.832Z,false
5f48dc593ed75a76be5ae30d,PR-HEAB14,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset C108,SkullCandy Headset Bluetooth Headset C108 Sold by VD-AB00SM01,Bluetooth Headset - White Sold by VD-AB00SM01,false,2000,INR,5,300,300,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae30e,PR-HEAB13,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset C258,SkullCandy Headset Bluetooth Headset C258 Sold by VD-AB00SM01,Bluetooth Headset - Blue Sold by VD-AB00SM01,false,2500,INR,5,500,500,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae30f,PR-HEAB10,VD-AB00SM01,JBL Headset,Wired headset with Mic W158,JBL Headset Wired headset with Mic W158 Sold by VD-AB00SM01,Wired Headset with Mic- Blue Sold by VD-AB00SM01,false,1700,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae310,PR-HEAB16,VD-AB00SM01,SkullCandy Headset,Wired headset W158,SkullCandy Headset Wired headset W158 Sold by VD-AB00SM01,Wired Headset - Blue Sold by VD-AB00SM01,false,1100,INR,5,700,700,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae311,PR-HEAB18,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset with Mic C501,SkullCandy Headset Bluetooth Headset with Mic C501 Sold by VD-AB00SM01,Bluetooth Headset with Mic - Black Sold by VD-AB00SM01,false,5000,INR,5,5,5,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae312,PR-HEAB8,VD-AB00SM01,JBL Headset,Bluetooth Headset with Mic C108,JBL Headset Bluetooth Headset with Mic C108 Sold by VD-AB00SM01,Bluetooth Headset with Mic - White Sold by VD-AB00SM01,false,4000,INR,5,30,30,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae313,PR-HEAB17,VD-AB00SM01,SkullCandy Headset,Wired headset W201,SkullCandy Headset Wired headset W201 Sold by VD-AB00SM01,Wired Headset - White Sold by VD-AB00SM01,false,1200,INR,5,10,10,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae314,PR-HEAB20,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset with Mic C101,SkullCandy Headset Bluetooth Headset with Mic C101 Sold by VD-AB00SM01,Bluetooth Headset with Mic - White Sold by VD-AB00SM01,false,4000,INR,5,30,30,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae315,PR-HEAB19,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset with Mic C251,SkullCandy Headset Bluetooth Headset with Mic C251 Sold by VD-AB00SM01,Bluetooth Headset with Mic - Blue Sold by VD-AB00SM01,false,4500,INR,5,20,20,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae316,PR-HEAB6,VD-AB00SM01,JBL Headset,Bluetooth Headset with Mic C501,JBL Headset Bluetooth Headset with Mic C501 Sold by VD-AB00SM01,Bluetooth Headset with Mic - Black Sold by VD-AB00SM01,false,5000,INR,5,5,5,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae317,PR-HEAB7,VD-AB00SM01,JBL Headset,Bluetooth Headset with Mic C258,JBL Headset Bluetooth Headset with Mic C258 Sold by VD-AB00SM01,Bluetooth Headset with Mic - Blue Sold by VD-AB00SM01,false,4500,INR,5,20,20,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae318,PR-HEAB12,VD-AB00SM01,SkullCandy Headset,Bluetooth Headset C508,SkullCandy Headset Bluetooth Headset C508 Sold by VD-AB00SM01,Bluetooth Headset - Black Sold by VD-AB00SM01,false,3000,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae319,PR-HEAB11,VD-AB00SM01,JBL Headset,Wired headset with Mic W208,JBL Headset Wired headset with Mic W208 Sold by VD-AB00SM01,Wired Headset with Mic- White Sold by VD-AB00SM01,false,1800,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae31a,PR-HEAB22,VD-AB00SM01,SkullCandy Headset,Wired headset with Mic W151,SkullCandy Headset Wired headset with Mic W151 Sold by VD-AB00SM01,Wired Headset with Mic- Blue Sold by VD-AB00SM01,false,1700,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae31b,PR-HEAB15,VD-AB00SM01,SkullCandy Headset,Wired headset W108,SkullCandy Headset Wired headset W108 Sold by VD-AB00SM01,Wired Headset - Black Sold by VD-AB00SM01,false,1000,INR,5,400,400,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae31c,PR-HEAB21,VD-AB00SM01,SkullCandy Headset,Wired headset with Mic W101,SkullCandy Headset Wired headset with Mic W101 Sold by VD-AB00SM01,Wired Headset with Mic- Black Sold by VD-AB00SM01,false,1500,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
5f48dc593ed75a76be5ae31d,PR-HEAB24,VD-AB00SM01,JBL Headset,Bluetooth Headset C501,JBL Headset Bluetooth Headset C501 Sold by VD-AB00SM01,Bluetooth Headset - Black Sold by VD-AB00SM01,false,3000,INR,5,100,100,5,Y,system,Tue Aug 18 2020 10:37:21 GMT+0530 (India Standard Time)1,system,2020-08-11T09:23:31.426Z,false
db.inventory.aggregate([
  {
    $search: {
	      "text": {
        "query": "JBL",
        "path": "productName"
      }
    }
  },
  {
  $group:{_id:"$category",Total:{$sum:1}} 
  } 
]);

This doesn’t give an error because you didn’t include the index?

Hello, everyone! :wave:

I have modeled the issue using this dataset:

db.products.insertMany([
  {
    _id: 'A',
    category: 'speakers',
    productName: 'JBL Flip Essential'
  },
  {
    _id: 'B',
    category: 'speakers',
    productName: 'JBL Boombox 2 Squad'
  },
  {
    _id: 'C',
    category: 'headphones',
    productName: 'Sennheiser HD 350 BT'
  },
  {
    _id: 'D',
    category: 'headphones',
    productName: 'JBL Tune 520'
  },
  {
    _id: 'E',
    category: 'headphones',
    productName: 'JBL Tune 710'
  },
  {
    _id: 'F',
    category: 'headphones',
    productName: 'Sennheiser Epos Adapt'
  },
]);

If Atlas Search text index on field produdctName is specified, the aggregation below

db.products.aggregate([
  {
    $search: {
      text: {
        query: 'jbl',
        path: 'productName'
      }
    }
  },
  {
    $group: {
      _id: '$category',
      total: {
        $sum: 1,
      }
    }
  }
]);

produces expected result:
[ { _id: 'speakers', total: 2 }, { _id: 'headphones', total: 2 } ]

Index used:

{
    "mappings": {
      "dynamic": false,
      "fields": {
        "productName": {
          "type": "string",
        }
      }
    }
  },

If we drop the index - empty result will be returned and no error is thrown (executed in shell).

There is no error. But there is no output as well. if $group is remove from the command, there is an output. Kindly let me know if there is other options to do grouping.

No idea on this one. Since $search produces results, $group stage must output something. Even if we try to use non-existent field name.