簡體   English   中英

貓鼬:查找子文檔並修改它的數組?

[英]Mongoose: Find SubDocument And Modify It's Array?

基本上,我試圖在Shotgun npm模塊中為Node.js創建命令,以在數據庫中找到用戶,並禁止該用戶(如果存在)。

這是一個示例用戶條目(用戶存儲在名為“用戶”的集合中):

{
"__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
                        };
                    });
            };

如您所見,這是基本要點,我在尋找用戶以檢查其是否存在以及修改其數組方面遇到問題。 如何更改此代碼,以便輸入用戶名提示符的文本隨后用於在數據庫的“用戶”集合中搜索數據庫中的現有用戶,然后“禁止”添加到他的“角色”數組中?

編輯:我現在意識到,我只需要將它們轉換為貓鼬,我就認為第一部分是shell.db.User.findOne({username:options.username},function(err,user){這找到了用戶根據提示,當然是選項。

要在mongoDb Shell中執行請求的操作,您需要調用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM