[DOCS-2901] $text in aggregation doesn't say it must be the first stage Created: 11/Mar/14  Updated: 11/Jan/17  Resolved: 11/Mar/14

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Task Priority: Major - P3
Reporter: William Cross Assignee: Shannon Bradshaw (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 9 years, 49 weeks, 1 day ago

 Description   

http://docs.mongodb.org/master/tutorial/text-search-in-aggregation/

It says, under "Restrictions," that a $text search can only happen once in the aggregation pipeline:

A text operator can only occur once in the stage.

This is technically true, but only because the $text search must happen first. I tried to $project first, then $text search to see if it would work, and I got an error:

> db.bible.aggregate([ { $match: {$text: { $search : "Belshazzar humbled" } } } , { $group: {_id: "$_id.chapter", count : {$sum: 1}}}] )
{ "_id" : "16", "count" : 1 }
{ "_id" : "33", "count" : 1 }
{ "_id" : "22", "count" : 1 }
{ "_id" : "3", "count" : 1 }
{ "_id" : "1", "count" : 1 }
{ "_id" : "4", "count" : 1 }
{ "_id" : "7", "count" : 1 }
{ "_id" : "36", "count" : 1 }
{ "_id" : "18", "count" : 1 }
{ "_id" : "5", "count" : 3 }
{ "_id" : "11", "count" : 1 }
{ "_id" : "10", "count" : 1 }
{ "_id" : "2", "count" : 3 }
{ "_id" : "12", "count" : 4 }
{ "_id" : "25", "count" : 1 }
{ "_id" : "32", "count" : 1 }
{ "_id" : "8", "count" : 1 }
> db.bible.aggregate([{$project: {text: 1}}, { $match: {$text: { $search : "Belshazzar humbled" } } } ] )
assert: command failed: {
	"errmsg" : "exception: $match with $text is only allowed as the first pipeline stage",
	"code" : 17313,
	"ok" : 0
} : aggregate failed
Error: command failed: {
	"errmsg" : "exception: $match with $text is only allowed as the first pipeline stage",
	"code" : 17313,
	"ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1098:12)
    at (shell):1:10
2014-03-11T14:25:03.363-0400 Error: command failed: {
	"errmsg" : "exception: $match with $text is only allowed as the first pipeline stage",
	"code" : 17313,
	"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
>



 Comments   
Comment by Kay Kim (Inactive) [ 11/Mar/14 ]

already has the line:

Restrictions

Text search in the aggregation pipeline has the following restrictions:

The $match stage that includes a $text must be the first stage in the pipeline.

Generated at Thu Feb 08 07:44:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.