![](/img/trans.png)
[英]Is this a safe way for inserting and updating an array of objects in mongodb?
[英]Updating array in mongodb in safe mode
我想更新mongodb中的數組元素。 在mongodb shell中,它可以工作:
db.ipolls.update({_id:"5Qu9fXG84tNSZo7sv","players.label":"R1"},{$inc:{"players.$.score":1}});
但是當我在流星上運行時:
Ipolls.update( {_id:pollster,"players.label":notChosen.label},{$inc:{"players.$.comparisons":1}});
我收到錯誤:未捕獲的錯誤:不允許。 不受信任的代碼只能按ID更新文檔。 [403]
是否可以在客戶端運行此查詢?
在客戶端上,您只能將_id
字段用作選擇器。 您已使用{_id:pollster,"players.label":notChosen.label}
這是流星,只是使其變得更安全。 從理論上講,您可以創建一個怪異的選擇器,然后從.allow
規則檢查中獲取信息。
首先查詢文檔,然后使用該文檔進行更新:
var doc_to_update = Ipolls.findOne({_id:pollster,"players.label":notChosen.label});
Ipolls.update( {_id: doc_to_update._id},{$inc:{"players.$.comparisons":1}});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.