猫鼬:查找SubDocument并修改它的数组?

时间:2014-05-13 04:38:15

标签: javascript arrays node.js mongodb mongoose

基本上我正在尝试在Shotgun npm模块中为Node.js创建一个命令,该命令在数据库中找到一个用户,如果他存在则禁止他。

这是一个示例用户条目(用户存储在名为' users'的集合中):

{
"__v" : 0,
"_id" : ObjectId("536d1ac80bdc7e680f3436c0"),
"joinDate" : ISODate("2014-05-09T18:13:28.079Z"),
"lastActiveDate" : ISODate("2014-05-09T18:13:48.918Z"),
"lastSocketId" : null,
"password" : "Johndoe6",
"roles" : [], //the 'banned' will goe here, ex. ['banned']
"username" : "johndoe6"
}

这是我的命令js代码:

exports.roles = 'mod, admin'; //this command is only to be used by users who are admins or mods
exports.description = "Sets the specified user's ban status."; //info for help menu
exports.usage = "<USER>"; //also more help into
exports.options = { //start of username prompt
    username: { //name of the option
    required: true,
    prompt: "Please enter a user username to edit.",
    noName: true,
    hidden: true,
    validate: /^[a-z0-9_-]+$/i,
},
confirm: { //this is a yes/no prompt for adding to the array
description: "Confirms user ban.",
validate: function (confirm, shell, options) {
        var confirmRegex = /^y(es)?|true$/i;
        options.confirm = confirmRegex.test(confirm);
        return options.confirm ? true : "User ban canceled.";
    }
}
};
 exports.invoke = function (shell, options) { //shotgun thing
 shell.db.users.find({ "username" : options.username }); //find username based on what was plugged in for 'options.username' which was our prompt
 if (err) return shell.error(err);
 if (!user) return shell.error("No user with username {{0}} exists.".format(options.username));
 shell.getCurrentUser(function (currentUser) { 
        // If user did not supply confirm already then set the CLI text to edit and          prompt for user confirm.
       if (options.username.isModOrAdmin()){ //check if a user has 'mod' or 'admin' in array 'roles'
        return shell.error("You cannot ban other mods or admins".format(options.username))
}
if (!options.confirm) {
            shell.log("Modify user {{0}} confirm.".format(options.username));
            shell.setPrompt('confirm', 'editUser', options);
        }
        else {
            // If we have user confirm then modify the user.
            user.username = options.confirm;
                    if (err) return shell.error(err);
                    shell.db.users.update({ "username" : options.username }, {$addToSet: {roles: "banned"}}); //go into the database, find the user we typed into the prompt and add a 'banned' into its 'roles' array
                    shell.log("User {{0}} banned        successfully.".format(options.username)); //notify command user that ban happened
                        };
                    });
            };

正如你所看到的,这是基本的要点,我只是在找到用户检查他是否存在以及修改他的数组时遇到问题。如何更改此代码,以便输入用户名提示中的文本然后用于在数据库中搜索用户&#39;中的现有用户?收集我的数据库然后被禁止&#39;加入他的角色&#39;阵列

编辑:我现在意识到我只需要将它们转换为Mongoose 我觉得第一部分倒了 shell.db.User.findOne({username:options.username},function(err,user){ 这会根据提示找到用户,这当然是options.username

1 个答案:

答案 0 :(得分:1)

要在mongoDb Shell中执行请求的操作,您需要调用:

db.users.findAndModify({"username" : options.username},
                       [],
                       {$addToSet: {roles: "banned"}},
                       {'new':true}
);