0

My room documents in cosmos DB looks like this:

{
 roomId: 'id1'
 name: 'room1'
},
{
 roomId: 'id2'
 name: 'room2'
},
...

From nodejs application, I have an array of roomIds and I want to retrieve all the related documents from cosmosDB.

since cosmos has IN operator, I used the following SQL query to retrieve the documents and this works fine.

const querySpec = {
  query: `SELECT * from c where c.roomId in ('${roomIdCollection.join("','")}') ORDER BY c._ts DESC`,
};

However, when I try parameters binding it doesn't work!

const querySpec = {
  query: `SELECT * from c where c.roomId in @rooms ORDER BY c._ts DESC`,
  parameters: [
    { name: "@rooms", value: roomIdCollection }
  ]
};  

const { resources } = await client.database(DB_NAME)
  .container(ROOM_CONTAINER_NAME)
  .items.query(querySpec)
  .fetchAll();

for obvious reasons I don't want to concatenate my SQL query string, instead, I want to use querySpec parameters binding. how can I do that?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Morteza Adi
  • 2,413
  • 2
  • 22
  • 37

0 Answers0