如何解决MongoDB /领域-使用FindOneAndUpdate-匹配_id
我正在关注https://hugo-blog-ilbfw.mongodbstitch.com/stitching-sheets/上的教程,该教程显示了如何通过Stitch / Realm函数和webhook将Google表格数据导出到MongoDB。该示例运行完美,但是当您多次导出时,它会加载越来越多的文档-我希望能够修改现有文档并插入新文档。
我一直在尝试使用{ upsert: true }
的findOneAndUpdate方法https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/,但是我从Realm函数中得到了一个错误;我确定这是我犯的一个非常基本的错误。
我的Google Sheets js是:
// Create an object which contains keys for each column in the spreadsheet
var columns = { // 0 indexed
type: 2,date_start: 3,date_end: 4,loc: 5,desc: 0,url: 1,status: 6,owner: 7,event_id: 8
}
/****
* Export the events from the sheet to a MongoDB Database via Stitch
****/
function exportEventsToMongoDB() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Events");
var headerRows = 1; // Number of rows of header info (to skip)
var range = sheet.getDataRange(); // determine the range of populated data
var numRows = range.getNumRows(); // get the number of rows in the range
var data = range.getValues(); // get the actual data in an array data[row][column]
for (var i=headerRows; i<numRows; i++) {
var eventIdCell = range.getCell(i+1,columns.event_id+1);
var desc = data[i][columns.desc];
var date_start = Utilities.formatDate(new Date(data[i][columns.date_start]),"GMT","yyyy-MM-dd'T'HH:mm:ss'Z'");
var date_end = Utilities.formatDate(new Date(data[i][columns.date_end]),"yyyy-MM-dd'T'HH:mm:ss'Z'");
// Make a POST request with form data.
var formData = {
'name': data[i][columns.desc],'location': data[i][columns.loc],'date_start': date_start,'date_end': date_end,'status': data[i][columns.status],'owner': data[i][columns.owner],'url': data[i][columns.url],'type': data[i][columns.type],'event_id': data[i][columns.event_id]
};
var options = {
'method' : 'post','payload' : formData
};
if (desc) {
var insertID = UrlFetchApp.fetch('https://webhooks.mongodb-realm.com/api/client/v2.0/app/event-fvyrn/service/myfunc123xx/incoming_webhook/AddEvent',options);
eventIdCell.setValue(insertID); // Insert the new event ID
}
}
}
我的Stitch / realm函数是:
exports = async function(payload) {
const mongodb = context.services.get("mongodb-atlas");
const eventsdb = mongodb.db("events");
const eventscoll = eventsdb.collection("events");
//const result= await eventscoll.insertOne(payload.query);
const result= await eventscoll.updateOne({ _id: ObjectID(event_id)
},payload.query,{ upsert: true })
var id = result.insertedId.toString();
if(result) {
return JSON.stringify(id,false,false);
}
return { text: `Error saving` };
}
我第一次与
const result= await eventscoll.insertOne(payload.query);` in place of the `findOneAndUpdate` method so the Google doc has an event id which matched the Mongo `_id`
但是,当我将const result= await eventscoll.insertOne(payload.query);
替换为
const result= await eventscoll.findOneAndUpdate({ _id: event_id },{ upsert: true });
或updateOne
并执行查询,我在Google表格中收到一条错误消息:
Request failed for https://webhooks.mongodb-realm.com returned code 400. Truncated server response: {"error":"{\"message\":\"'event_id' is not defined\"}","error_code":"FunctionExecutionError","link":"https://realm.mongodb.com/groups/5f4033de3f8ad... (use muteHttpExceptions option to examine full response)
有人可以告诉我我在做什么错吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。