Home > Back-end >  How do I return the collection that are stored inside the database of CosmosDB using nodejs
How do I return the collection that are stored inside the database of CosmosDB using nodejs

Time:12-16

Good afternoon, I am kind of new to using Azure. I have created a temporary database with a container of collections. In my code, I am already connected to the endpoint, primary key, database, and the container. Now, I would like to print the collections stored in the container of the database.

Here is the code

const config = require("./config")
const CosmosClient = require('@azure/cosmos').CosmosClient


const endpoint = config.endpoint
const masterKey = config.primaryKey


const client = new CosmosClient({
    endpoint: endpoint,
    auth: {masterKey: masterKey}
})

const HttpStatusCode = { NOTFOUND: 404}

const databaseId = config.database.id;
const containerId = config.container.id;

CodePudding user response:

You can use query().fetchAll() command to fetch all documents in a container.

const client = new CosmosClient({ endpoint: cosmos.endpoint, key: cosmos.key });
const container = client
    .database(cosmos.databaseId)
    .container(cosmos.containerId);

const querySpec = { query: "SELECT * FROM c" };

// read all items in the container
const { resources } = await container.items.query(querySpec).fetchAll();
  • Related