I use collection.updateOne({id}, {$addToSet: {pictures: picture}}, {upsert: true}); to update existed data based on id or insert the data, but I found two pieces of data with the same id in my database.I expect that when I find that there is already data for this id, I will update the data instead of adding it.
Is it caused by concurrent requests?
app.get('/aaa', async (req, res) => {
await client.connect();
const {id, picture} = req.query;
const collection = client.db('abc').collection('bcd');
await collection.updateOne({id}, {$addToSet: {pictures: picture}}, {upsert: true});
res.send('response');
});
useEffect(() => {
fetch('http://localhost:3000/aaa?id=1&picture=000');
fetch('http://localhost:3000/aaa?id=1&picture=111');
fetch('http://localhost:3000/aaa?id=2&picture=123');
fetch('http://localhost:3000/aaa?id=3&picture=123');
}, []);
I tested it with the demo above and it didn’t work.