简体   繁体   English

如何连接两个MongoDb collections?

[英]How to connect two MongoDb collections?

I need to know how to add two database collections in MongoDB. Here is my code我需要知道如何在 MongoDB 中添加两个数据库 collections。这是我的代码

async function run2(){
try {
    await client.connect();
  
    const itemCollection2 = client.db('warehouse_inventory').collection('myCollection');

    // for my items
    app.post('/items', async (req, res)=>{
        const newItem = req.body;
        const result = await itemCollection2.insertOne(newItem);
        res.send(result);
    })
   

}
finally {

}

} }

to add or create others database just take a const and change collaction name it will be create database automaticely.添加或创建其他数据库只需使用 const 并更改集合名称,它将自动创建数据库。 to use use that const name to your api cursor将该常量名称用于您的 api cursor

async function run2(){
try {
   await client.connect();
   const otheritemCollection = 
   client.db('warehouse_inventory').collection('myCollection2');

    // for my other db
    app.post('/items', async (req, res)=>{
    const newItem = req.body;
    const result = await otheritemCollection.insertOne(newItem);
    res.send(result);
   })

}
finally {

} } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM