Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you want to find an element inside of a list in MongoDB document you can use the simple find command. Let's say you have the below documents:
[{
"food": "Ghorme",
"creation_date": "2017-10-12 12:00:00"
"rates": ["bad", "so_so", "not_bad", "mmmm"]
},{
"food": "Kebab",
"creation_date": "2017-10-07 22:10:00"
"rates": ["great", "not_bad", "great", "great"]
}]

Now if you want to find a document that has the value of bad in rates, you just use the below find query:
db.foods.find({rates: "bad" }).pretty()

#mongodb #mongo #find #list #array
You can use regex when you want to query MongoDB by using RegExp:
db.users.find({username: RegExp('ali.*')})

The above query will find users with username that starts with ali.

#mongo #mongodb #regexp #find
How to find documents with a specific field type?

It may happen to have different field types in a specific field like credit, and some of them are numeric and some of them string. In order to find NumberLong() field types you can use $type:

db.users.find({credit: {$type: "long" }})


If you want to remove those fields, if applicable, use remove instead of find to remove those documents that has wrong types. It is not sensible to do that for users document though, it just gives you the idea.

#mongodb #mongo #type #field_type #remove #find
In Pycharm I wrote something like below in multiple lines in the file:


method_name='get_account'


I wanted to add _v2 to all the method names, what I did was to use regex in PyCharm replace functionality. Press Command+R in order to open replace dialog. In the dialog there is an option called Regex, tick the checkbox in front of it and in find section write:


method_name='(.*)'


It will find all lines which has different names: .* and put that in a variable. (you can put something you have found in a variable by using parenthesis).

Now we can access the variable using $1. We now need to put the below code in replace section:


method_name='$1_v2'


The above code will put method name using $1 and the append _v2 to all the methods.


#pycharm #regex #find #replace
How to check if a field exists in MongoDB and it's value is not empty?

db.users.find({ profile_image: {$exists: 1, $ne: ""}  }, { profile_image:1 })

NOTE: $ne makes sure that field is not empty and $exists check whether field exist or not.


#mongodb #mongo #find #exists #ne
Delete files older than X days. You can use find command in order to find files with specific patterns in a specific directory and then remove those files:

BACKUP_DIR=/var/backup

# Number of days to keep backups
KEEP_BACKUPS_FOR=30 #days

find $BACKUP_DIR -type f -name "*.sql.gz" -mtime +$KEEP_BACKUPS_FOR -exec rm {} \;

-mtime n[smhdw]:
If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next full 24-hour period, is n 24-hour periods.

-exec utility [argument ...] ;
True if the program named utility returns a zero value as its exit status. Optional arguments may be passed to the utility. The expression must be terminated by a semicolon (``;''). If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator. If the string ``{}'' appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file. Utility will be executed from the directory from which find was executed. Utility and arguments are not subject to the further expansion of shell patterns and constructs. SO BECAREFUL SEMICOLON IS NEEDED!

#find #rm #exec #mtime #remove_old_files #remove
In MongoDB you can compare one field to another using $expr:

db.users.find({ $expr: { $eq: ["$created_at", "$updated_at"] } })

Here we get users that their updated_at field is equal to created_at field, here it means that user has not yet updated his profile.

#mongodb #mongo #expr #find
How to recursively rename filenames?

find . -name '*txt' -exec bash -c ' mv $0 ${0/brand-/category-}' {} \;


The above command renames txt files starting with brand- to category-.

#linux #bash #find #rename #batch_rename