How to Search text and returns collection based on that

I am on atlas free tier with version → 5.0.7

I am trying to build Chat system and for that I used
dmId like this “recipient_id-sender-id”

my objec looks like this

_id:62595a1c8c0fce4f9dbfc99e
content:DM Id added to db
creatorId:624f98d1db10f12db205b263
recipientId:6257e81d2a74d7363f621370
dmId:6257e81d2a74d7363f621370-624f98d1db10f12db205b263
postId:6258206b1f681ed05547fa90
createdAt:2022-04-15T11:42:20.753+00:00

So from nodejs backend I try to fetch data

return db
    .collection('dms')
    .aggregate([
      {
        $text: { $search: currentUser }
      },
      { $sort: { _id: -1 } },
      { $limit: limit },
      {
        $lookup: {
          from: 'users',
          localField: 'creatorId',
          foreignField: '_id',
          as: 'creator',
        },
      },
      { $unwind: '$creator' },
      { $project: dbProjectionUsers('creator.') },
    ])
    .toArray();
}

but this gives me this error -

$text is not allowed in this atlas tier

any other possible solutions possilbe
thanks

Hi @anish_jain,

but this gives me this error -
$text is not allowed in this atlas tier

Thanks for confirming your Atlas tier details and providing the aggregation pipeline in use. The above error is a bit odd as the $text operator you have used will not work in the pipeline provided in it’s current format. However, I have taken note of this as it may cause some confusion. In saying so, the pipeline will require a $match stage that contains the $text operator at the beginning similar to the following:

return db
    .collection('dms')
    .aggregate([
     {
         $match: { $text: { $search: currentUser } } 
     },
...

Additionally, could you provide the use case for using the $text operator in Atlas deployment? Since this is an Atlas deployment, I would recommend taking a look at using Atlas Search which allows fine-grained text indexing and querying of data on your Atlas cluster. You may find the following documentation useful regarding Atlas Search:

I would also recommend reviewing the Text Search in the Aggregation Pipeline documentation which provides another example of the $text operator being used in an aggregation pipeline.

Hope the above is useful.

Regards,
Jason

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.