next.js 如何更新Mongo DB中的许多数据?

kqqjbcuj  于 2023-02-22  发布在  Go
关注(0)|答案(3)|浏览(117)

我无法在MongoDB中更新许多数据(对象类型的数组数据)请帮助我

import {MongoClient, ObjectId} from "mongodb";

async function handler(req, res) {
    if (req.method === "POST") {
        const data = req.body;
        const client = await MongoClient.connect("mongodb+srv:myURL")
        const db = client.db();
        const postItCollection = db.collection("tableData");
        const result = await postItCollection.updateMany({_id: new ObjectId(data.id)}, {$set : {
                contents:data.contents,
                width:data.width,
                height:data.height,
                positionX:data.positionX,
                positionY:data.positionY,
                positionZ:data.positionZ,
            }}, {upsert: true});
        client.close();
        res.status(201).json({message: "success"})
    }
}

export default handler;

例如,我有一个长度为2的数组数据。它是由需要更新的对象数据组成的。如果我使用上面的代码,所有属性值都输入空...
如何一次更新多个阵列数据?

[
  {
    id: 0.5849485030977961,
    positionX: 0,
    positionY: 0,
    positionZ: 10,
    width: 200,
    height: 220,
    userId: 'userid',
    style: '',
    pinned: false,
    contents: { titles: [Array], contents: [Array] }
  },
  {
    id: 0.06866579058492106,
    positionX: 0,
    positionY: 0,
    positionZ: 10,
    width: 200,
    height: 220,
    userId: 'userid',
    style: '',
    pinned: false,
    contents: { titles: [Array], contents: [Array] }
  }
]

如果我有以上数据,我想根据Mongo db

中的每个属性上传

pieyvz9o

pieyvz9o1#

import {MongoClient, ObjectId} from "mongodb";

async function handler(req, res) {
    if (req.method === "POST") {
        const data = req.body;
        const client = await MongoClient.connect("mongodb+srv://URL")
        const db = client.db();
        const postItCollection = db.collection("tableData");
        for(let i=0; i<data.length; i++){
        const result = await postItCollection.updateMany({_id: new ObjectId(data[i].id)}, {$set : {
                pinned:data[i].pinned,
                style:data[i].style,
                userId:data[i].userId,
                contents:data[i].contents,
                width:data[i].width,
                height:data[i].height,
                positionX:data[i].positionX,
                positionY:data[i].positionY,
                positionZ:data[i].positionZ,
            }}, {upsert: true});
        }
        client.close();
        res.status(201).json({message: "success"})
    }
}

export default handler;

嗨我解决🤣了关键是for loop
但是如果你有一个干净的代码,请让我知道🙏

4ktjp1zp

4ktjp1zp2#

请参考MongoDB中的此文档,以便在单个查询中更新多个。
https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/

qhhrdooz

qhhrdooz3#

可以使用findByIdAndUpdate简化代码:

async function handler(req, res) {
  if (req.method === 'POST') {
    const data = req.body;
    const client = await MongoClient.connect('mongodb+srv://URL');
    const db = client.db();
    const postItCollection = db.collection('tableData');
    for (const d of data) {
      await postItCollection.findByIdAndUpdate(new ObjectId(d.id), d, {
        upsert: true,
      });
    }
    client.close();
    res.status(201).json({ message: 'success' });
  }
}

相关问题