Azure cosmosdb 连接到多个SDB文档

Azure cosmosdb 连接到多个SDB文档,azure-cosmosdb,Azure Cosmosdb,我正在尝试在Cosmos中创建多个文档,其中一个文档在提交表单后将保存提交表单中的一些数据。我正在尝试使用一些其他文档来保存下拉选择列表中的数据。如何连接到多个config.containerId以读取一些数据,然后写入一些数据?我目前只能读/写一个 谢谢你的帮助 const config = {}; config.host = process.env.HOST || "https://localhost:8081"; config.authKey = process.env.AUTH_K

我正在尝试在Cosmos中创建多个文档,其中一个文档在提交表单后将保存提交表单中的一些数据。我正在尝试使用一些其他文档来保存下拉选择列表中的数据。如何连接到多个config.containerId以读取一些数据,然后写入一些数据?我目前只能读/写一个

谢谢你的帮助

const config = {};

config.host = process.env.HOST || "https://localhost:8081";
config.authKey =
  process.env.AUTH_KEY || "key";
config.databaseId = "ToDoList";
config.containerId = "Items";
config.containerId2 = "List";

if (config.host.includes("https://localhost:")) {
  console.log("Local environment detected");
  console.log("WARNING: Disabled checking of self-signed certs. Do not have this code in production.");
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  console.log(`Go to http://localhost:${process.env.PORT || '3000'} to try the sample.`);
}

module.exports = config;
其余代码如下所示:


只需实例化另一个TaskDao即可连接到第二个容器。请按照以下步骤操作:

请确保您已经遵循,并且您可以在本地成功运行该网站,因为我的所有代码修改都基于此演示

在本例中,我有一个名为ToDoList的DB,它有两个集合项和项2

转到config.js并为Items2添加两个配置:

转到app.js,实例化TaskDao2:

最后,转到routes/tasklist.js,修改构造函数方法如下:

   constructor(taskDao,taskDao2) {
     this.taskDao = taskDao;
     this.taskDao2 = taskDao2;
   }
完成此步骤后,您的应用程序可以成功连接到另一个收藏。添加任务时,我将相同的数据写入items2集合,转到addTask方法并添加以下代码:

await this.taskDao2.addItem(item);
好的,让我们启动web应用程序并添加任务:

检查cosmos数据库中的数据:


如您所见,现在可以将数据写入另一个集合。希望能有帮助

那么您有两个容器,您想在node.js项目中读/写这两个容器的DBs吗?是的,我想让它从一个容器中读取项目。然后在提交后将项目写入第二个容器。我们可以从多个表中获得查询结果吗?并创建querySpec1和querySpec2?例如:app.get'/',req,res,next=>taskList.List1req,res.catchnext app.get'/',req,res,next=>taskList.List2req,res.catchnext
config.databaseId2 = "ToDoList";
config.containerId2 = "Items2";
 const taskDao2 = new TaskDao(cosmosClient, config.databaseId2, config.containerId2)
 taskDao
   .init(err => {
     console.error(err)
   })
   .catch(err => {
     console.error(err)
     console.error(
       'Shutting down because there was an error settinig up the database.'
     )
     process.exit(1)
   })
 taskDao2
 .init(err => {
   console.error(err)
 })
 .catch(err => {
   console.error(err)
   console.error(
     'Shutting down because there was an error settinig up the database.'
   )
   process.exit(1)
 })

 const taskList = new TaskList(taskDao,taskDao2)
   constructor(taskDao,taskDao2) {
     this.taskDao = taskDao;
     this.taskDao2 = taskDao2;
   }
await this.taskDao2.addItem(item);