Dev01
Dev01

Reputation: 14169

Do I need to add other index keys to an Atlas Search Index?

I want to use MongoDB Atlas Search. I have the following query:

const reservationIDs = [...];
const channels = [...];
const keyword = "some text";
const indexName = "search";
const query = {
    _id: {$in: reservationIDs},
    source: {$in: channels},
    $search: {
        index: indexName,
        text: {
            query: keyword
        }
  }
};
const reservations = await Reservation.find(query).lean();

And I created this Atlas Search Index on mongodb.com:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "type": "string"
      },
      "message": {
        "type": "string"
      }
    }
  }
}

Do I need to add indexes for _id and source somehow?

{
    _id: 1,
    source: 1
}

Also, is this the recommended way to query an Atlas Search Index?

Upvotes: 0

Views: 320

Answers (1)

Roy Kiesler
Roy Kiesler

Reputation: 36

You have to use db.collection.aggregate to make Atlas Search queries. You can refer to https://www.mongodb.com/docs/atlas/atlas-search/searching/ for examples.

Upvotes: 1

Related Questions