If you want to find an element inside of a list in
Now if you want to find a document that has the value of
#mongodb #mongo #find #list #array
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
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
If you want to remove those fields, if applicable, use
#mongodb #mongo #type #field_type #remove #find
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
I wanted to add
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
The above code will put method name using $1 and the append
#pycharm #regex #find #replace
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
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:
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.
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
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