https://i.stack.imgur.com/7Tq6v.png
import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
if(req.method === "POST"){
const data = req.body;
const client = await MongoClient.connect("mongoDB URL")
const db = client.db();
const postItCollection = db.collection("postIts");
const result = await postItCollection.updateOne({_id: ObjectId(data.id)}, {$set:{positionX:data.y, positionY:data.x}});
console.log(result);
client.close();
res.status(201).json({message:"success"})
}
}
export default handler;
- 我附上一张图片以防万一。我已经找了三个小时,但我找不到答案....我想地球上没有人有和我一样的问题....
- 我尝试安装“bson-objectid”库和“mongoose”类型属性
import mongoose from "mongoose";
const { ObjectId } = mongoose.Types;
我试过了,但是我看到同样的错误“方法表达式不是函数类型”
帮帮我,地球上所有的主人。
新手送的。
import {MongoClient, ObjectId} from "mongodb";
async function handler(req, res){
if(req.method === "POST"){
const data = req.body;
const client = await MongoClient.connect("mongoDB URL")
const db = client.db();
const postItCollection = db.collection("postIts");
const result = await postItCollection.updateOne({_id: data.id}, {$set:{positionX:data.y, positionY:data.x}}); // <= if i not use ObjectId
console.log(result);
client.close();
res.status(201).json({message:"success"})
}
}
export default handler;
- 如果不使用ObjectId,我会收到成功消息,但找不到id...https://i.stack.imgur.com/XJOQH.png
- 我只想在mongoDB中找到id并更改数据
PS.我怎么能马上看到图片而不是网址呢?我看了其他的答案,做了同样的事情,但只有网址出来了...
1条答案
按热度按时间dhxwm5r41#
哦,我找到答案了...!这是
new
操作...How do I search for an object by its ObjectId in the mongo console?