gpt4 book ai didi

javascript - Meteor:通过 ID 更新数据库中的多个条目

转载 作者:行者123 更新时间:2023-11-29 19:39:58 25 4
gpt4 key购买 nike

我需要帮助更新数据库中多个条目的 meteor 行。我认为下面的第一个 Entries.update 不起作用,因为 meteor 现在需要您通过 id 进行更新。

'click #draw': ->
winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
if winner
Entries.update({recent: true}, {$set: {recent: false}}, {multi: true})
Entries.update(winner._id, $set: {winner: true, recent: true})
Template.entry.winner_class = ->
if this.recent then 'highlight' else ''

所以我试着改成下面的代码。但是,它无法正常工作,因为它看起来只更改了一个 ID(第一个)。

'click #draw': ->
winner = _.shuffle(Entries.find(winner: {$ne: true}).fetch())[0]
recent_winner = Entries.find(recent: true).fetch()
if winner
Entries.update(recent_winner._id, {$set: {recent: false}}, {multi: true})
Entries.update(winner._id, $set: {winner: true, recent: true})
Template.entry.winner_class = ->
if this.recent then 'highlight' else ''

如有任何帮助,我们将不胜感激。

最佳答案

您将需要通过 Meteor.methods 一次修改多个文档。来自 the documentation :

The behavior of update differs depending on whether it is called by trusted or untrusted code. Trusted code includes server code and method code. Untrusted code includes client-side code such as event handlers and a browser's JavaScript console.

Trusted code can modify multiple documents at once by setting multi to true, and can use an arbitrary Mongo selector to find the documents to modify. It bypasses any access control rules set up by allow and deny. The number of affected documents will be returned from the update call if you don't pass a callback.

Untrusted code can only modify a single document at once, specified by its _id. The modification is allowed only after checking any applicable allow and deny rules. The number of affected documents will be returned to the callback. Untrusted code cannot perform upserts, except in insecure mode.

编辑:

例如,方法调用可能看起来像这样:

"click #draw": function(){
var winner = _.shuffle(Entries.find({winner: {$ne: true}}).fetch())[0];
if (!!winner){
Meteor.call(
"drawWinner", //an arbitrary method name of your choosing
winner, // passing it your winner
function(error, result){ // an optional async callback
if (error){
// handle error if error from method
} else {
// handle any return object from method
}
}
);
}
}

然后在您的方法调用中,它可能被放置在共享目录中,例如“lib”或服务器端专用目录中(有关这一点的更多信息,请参阅 Meteor 文档):

Meteor.methods({
"drawWinner": function(winner){
Entries.update({recent: true}, {$set: {recent: false}}, {multi: true});
Entries.update(winner._id, {$set: {winner: true, recent: true}});
return winner; //or the like
}
});

关于javascript - Meteor:通过 ID 更新数据库中的多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com