![](/img/trans.png)
[英]Creating subcollection in Firestore generates two documents with same id (rather than updating the pre-existing document)
[英]MongoDB save() creating new document each time rather than updating existing document
我正在使用此功能保存到mongodb:
create(req, res, next) {
let data = req.body;
if(data) {
let newIssue = this.lib.db.model('Issues')('data');
console.log(newIssue);
newIssue.save((err, emp) => {
if(err) return next(this.RESTError('InternalServerError', err));
this.writeHAL(res, emp);
});
} else {
next(this.RESTError('InvalidArgumentError', 'No data received'));
}
}
這是我的架構:
module.exports = {
"id": "Issues",
"properties": {
"issue_title": {
"type": "string",
"description": "The title of the issue"
},
"issue_number": {
"type": "string",
"description": "The issue number"
},
"issue_url": {
"type": "string",
"description": "URL of the issue"
}
}
}
我對save()
理解是,如果該文檔已經存在於mongodb中,它將進行更新,但是如果不存在則創建一個新文檔。 但是,每次我發布同一文檔時,都會插入一個新文檔,而不是更新現有文檔。 我懷疑這是因為在保存時,包含唯一ID的_id
鍵被添加到我的架構中,從而使文檔變得唯一。 這是正確的嗎?如果是,該如何解決?
附加信息
我現在添加了一個PUT。 我的想法是我將使用{upsert:true}
進行更新。 我之所以這樣做,部分原因在於我可以issue/{id} where
{id} is the mongodb
_id`並更新現有文檔。
這是我的PUT:
update(req, res, next) {
let data = req.body;
let id = req.params.id;
if(id) {
this.lib.db.model('Issues')
.findOne({_id: id})
.exec((err, updateIssue) => {
if(err) return next(this.RESTError('InternalServerError', err));
updateIssue = Object.assign(updateIssue, data);
updateIssue.update((err, issue) => {
if(err) return next(this.RESTError('InternalServerError', err));
this.writeHAL(res, issue);
});
});
}
}
這是路由:
controller.addAction({
'path': '/issues/{id}',
'method': 'PUT',
'summary': "UPDATES the issues's information",
'params': [swagger.pathParam('id', 'The id of the issue', 'string'),
swagger.bodyParam('issue', 'the new information to update', 'string')],
'responseClass': 'Issues',
'nickname': 'updateIssue'
}, controller.update);
我認為我不能將其用於upsert,因為我沒有_id
開頭,因此無法創建路徑。 我的另一個選擇是使用github_id
。 但是,每當我嘗試更改到/issues/{github_id}
路由路徑或使用.findOne({github_id: id})
類的功能更新我的函數時,都會出現錯誤。 我需要能夠使用/issues/{github_id}
並檢查github_id
已經存在。 如果是這樣,請更新文檔,否則請創建一個新文檔。
進一步的信息。
我將更新功能更改為:
update(req, res, next) {
let data = req.body;
let id = req.params.id;
if(id) {
this.lib.db.model('Issues')
.findOne({_id: id})
.exec((err, updateIssue) => {
if(err) return next(this.RESTError('InternalServerError', err));
updateIssue = Object.assign(updateIssue, data);
updateIssue.update({_id: id}, data, (err, issue) => {
if(err) return next(this.RESTError('InternalServerError', err));
this.writeHAL(res, issue);
});
});
}
}
如果我在updateIssue.update({_id: id}, data, (err, issue) => {
行之后添加以下console.log
updateIssue.update({_id: id}, data, (err, issue) => {
:
console.log('id: ', id);
console.log('data: ', data);
console.log('err: ', err);
console.log('issue: ', issue);
我得到以下輸出:
id: 5b80307403eea4f14b06fe8c
data: { _id: '5b80307403eea4f14b06fe8c',
github_id: '347593262',
issue_title: 'test issue XY',
issue_number: '2',
issue_url: 'https://api.github.com/repos/PCTestOrg/test1/issues/2a',
__v: 0 }
err: null
issue: { n: 1, nModified: 0, ok: 1 }
我希望將issue_title
從test issue X
更新為test issue XY
,但是不會發生更新。 我在這里做錯了什么?
如果要創建或更新,請使用upsert更新
Model.update({_id: id}, obj, {upsert: true}, function (err) {...});
我無法進行upsert工作,因此在閱讀了這篇文章后最終獲得了以下內容:
update(req, res, next) {
let data = req.body;
let id = req.params.id;
if(id) {
this.lib.db.model('Issues')
.findOne({_id: id})
.exec((err, updateIssue) => {
if(err) return next(this.RESTError('InternalServerError', err));
updateIssue = Object.assign(updateIssue, data);
updateIssue.save((err, issue) => {
if(err) return next(this.RESTError('InternalServerError', err));
this.writeHAL(res, issue);
});
});
}
else if(data) {
let newIssue = this.lib.db.model('Issues')(data);
console.log(newIssue);
newIssue.save((err, issue) => {
if(err) return next(this.RESTError('InternalServerError', err));
this.writeHAL(res, issue);
});
} else {
next(this.RESTError('InvalidArgumentError', 'No data received'));
}
}
我敢肯定,如果有人可以告訴我,我將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.