![](/img/trans.png)
[英]How to pass react.js data via createContainer like meteor.js or relay.js
[英]Mongodb update and insert data at the same time | upsert, react.js, meteor.js
如果問題已經存在,我會嘗試更新問題,如果用戶添加新問題,我會嘗試插入新問題。
const onSave = () => {
if (creating) {
// ・・ when you create a new questions, this runs
} else if (editing) {
// when you edit questions, this runs
questions.forEach((question) => {
Measures.update(
{ _id: question._id },
{
$set: {
title: question.text,
type:
question.type === QuestionType.SHORT_ANSWER
? MeasureType.TEXT
: MeasureType.MULTIPLE_CHOICE,
typeOptions: { frequency },
shortCodeOptions: {},
entity: System.getCurrentEntity()
}
}
);
});
}
};
現在,我只是Measures.update()
來更新問題。 但我不僅想更新,而且還想將新問題插入到 Measures 集合中(如果剛剛添加的話)。
我查看了 mongodb 文檔upsert ,但需要更多幫助。 https://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/write-operations/upsert/
我試過了,但收到一條錯誤消息,提示“Mesures.updateMany() 不是一個函數”
Measures.updateMany(
{_id: question._id},
{
$set: {
title: question.text,
type:
question.type === QuestionType.SHORT_ANSWER
? MeasureType.TEXT
: MeasureType.MULTIPLE_CHOICE,
typeOptions: {frequency},
shortCodeOptions: {},
entity: System.getCurrentEntity(),
},
},
{upsert: true},
);
mongodb 版本5.0.6
您正在查看錯誤的文檔。 Meteor collections不是mongodb 驅動程序 collections。您想使用https://docs.meteor.com/api/collections.html#Mongo-Collection-upsert ,即,只需將update
替換為upsert
即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.