首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >猫鼬:找到SubDocument并修改它的数组?

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

Stack Overflow用户
提问于 2014-05-13 04:38:15
回答 1查看 335关注 0票数 0

基本上,我试图在Node.js的猎枪npm模块中创建一个命令,该命令可以在数据库中找到一个用户,如果他存在,就禁止他使用。

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

代码语言:javascript
运行
复制
{
"__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代码:

代码语言:javascript
运行
复制
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
                        };
                    });
            };

正如您可以看到的,这是基本的依据,我只是在找到用户来检查他是否存在,以及修改他的数组时遇到了问题。如何更改此代码,以便将输入到用户名提示符中的文本用于搜索数据库中的“用户”集合中的现有用户,然后将“禁用”添加到他的“角色”数组中?

编辑:我现在意识到,我只需要把它们转换成“猫鼬”,我想第一部分就是shell.db.User.findOne({ username: options.username },函数(err,user) ){这是基于提示符的用户,当然是options.username。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-13 05:03:08

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

代码语言:javascript
运行
复制
db.users.findAndModify({"username" : options.username},
                       [],
                       {$addToSet: {roles: "banned"}},
                       {'new':true}
);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23622897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档