简体   繁体   中英

How to connect two MongoDb collections?

I need to know how to add two database collections in MongoDB. Here is my code

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. to use use that const name to your 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 {

} }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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