【问题标题】:MongoDB $pull not workingMongoDB $pull 不工作
【发布时间】:2015-05-30 19:50:39
【问题描述】:

我正在构建一个 Meteor 应用程序,并且我有竞赛/参赛作品集。当有人参加比赛时,他们的 user_id 会通过 $addToSet 被推送到 Contest.entered_users 数组中。代码如下:

entryInsert: function(entryAttributes) {
    check(Meteor.userId(), String);
    check(entryAttributes, {
        contest_id: String
    });

    var user = Meteor.user();
    var entry = _.extend(entryAttributes, {
        user_id: user._id,
        user_name: user.profile.name,
        submitted: new Date(),
        submitted_day: moment().format('MMM D')
    });

    var currentContest = Contests.findOne(entryAttributes.contest_id);

    // Check to make sure that the person has not already entered the giveaway
    if (currentContest.entered_users.indexOf(entry.user_id) !== -1) {
        throw new Meteor.Error('invalid', "You have already entered the giveaway");
    } else {
        Contests.update(
            currentContest._id,
            {
                $addToSet: {entered_users: entry.user_id},
                $inc: {entries: 1}}
        );
    }


    // Create entry in order to get the entry id
    var entryId = Entries.insert(entry, function(err) {
        if (err) {
            alert(err.reason);
        }
    });



    return {
        _id: entryId
    }
}

我想在删除条目时从 Contest.entered_users 数组中删除个人用户 ID。我正在尝试使用 $pull 但它似乎没有工作......当我删除一个条目时,entry.user_id 仍在比赛.entered_users 数组中。以下是相关代码:

'click .entry-delete': function(e, tmpl) {
    e.preventDefault();

    var currentEntry = this;
    var currentEntryId = this._id;

    var contestId = Contests.findOne(currentEntry.contest_id);

    // Update the contest by removing the entry's useer_id from entered_users
    Meteor.call('contestRemoveEntry', contestId, currentEntry, function(error) {
        if (error) {
            alert(error.reason);
        }
    });

    Meteor.call('entryRemove', currentEntryId, function(error) {
        if(error) {
            alert(error.reason);
        }
    });
}

这里是比赛RemoveEntry 方法:

contestRemoveEntry: function(contestId, currentEntry) {
    Contests.update({ _id: contestId }, { $pull: { entered_users: currentEntry.user_id } } );
}

关于为什么这不起作用的任何想法?我尝试了其他 SO 解决方案,但似乎没有任何效果。

【问题讨论】:

  • _id 是不是和竞赛ID 是同一类型的?如果 _id 类型是 ObjectID,则比赛 ID 也需要属于该类型,以便查询匹配。另外,请确保 currentEntry.user_id 属性值具有与 enter_users 元素相同的类型
  • 如果比赛 ID 是字符串并且需要匹配 _id 作为 ObjectId 使用:{ _id: ObjectId(contestId) }
  • 我刚刚尝试使用 {_id: ObjectId(contestId) } 并收到此错误“未定义 ObjectId”。知道为什么吗?
  • 确保在使用之前声明了 ObjectId 对象:var ObjectId = require('mongodb').ObjectID;
  • 我刚刚尝试了Contests.update(contestId, { $pull: { entered_users: currentEntry.user_id } } ); ,它似乎正在工作......我可以发誓我早点尝试过。

标签: javascript arrays mongodb meteor


【解决方案1】:

看来这是使 $pull 工作的正确方法:

Contests.update(contestId, { $pull: { entered_users: currentEntry.user_id } } );

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 2011-01-26
    • 2018-04-16
    • 2019-07-15
    • 2021-07-27
    • 2014-11-09
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多