db.collection_name.updateOne(
{"array_of_docs.field_name": {$exists: 1}},
{$unset: {"array_of_docs.$[].field_name": ""}}
);
db.collection_name.updateOne(
{"array_of_docs.field_name": {$exists: 1}},
{$unset: {"array_of_docs.$[].field_name": ""}}
);
find /path/to/directory -maxdepth 1 -type f | wc -l
for f in * ; do mv -- "$f" "01. $f" ; done
db.COLLECTION_NAME.updateMany(
{},
[{$set: {
"target_field": "$source_field"
}}]
);
for f in *.png; do
mv -- "$f" "${f%.png}.jpg"
done
db.collection_name.aggregate([
{$match: {"target_field": {$exists: true}}},
{$addFields: {
"remove": {$regexFind: {
input: "$target_field",
regex: /(.){1,}string_to_match/
}}
}},
{$match: {"remove": {$exists: true} }},
{$addFields: {
"remove_len": {$strLenBytes: "$remove.match"}
}},
{$addFields: {
"clean": {$substr: [
"$target_field",
"$remove_len",
100000]}
}},
])
db.authors.aggregate([
{$project: {"array": {"$objectToArray": "$sub_doc_field_name"}}},
{$unwind: "$array"},
{$project: {
"key": "$array.k"
}},
{$group: {
_id: "$key",
count: {$sum: 1},
}},
{$addFields: {
key: "$_id",
}},
{$project: {
_id: 0,
}},
{$sort: {
key: 1,
}},
]);
db.collection.aggregate([
{$addFields: {
"year": {$regexFindAll: {
input: "$born_string",
regex: /\b\d{4}\b/
}},
}},
{$addFields: {
"year": {$arrayElemAt: ["$year",0]},
}},
{$addFields: {
"year": "$year.match",
}},
{$addFields: {
"year": {$toInt: "$year"},
}},
]);
db.COLLECTION_NAME.aggregate([
{$addFields: {
"CONCATENATED_STRING": {$reduce: {
"input": "$ARRAY_OF_STRINGS_FIELD_NAME",
"initialValue": "",
"in": {
"$concat": [
"$$value",
{"$cond": [{"$eq": ["$$value", ""]}, "", ", "]},
"$$this"
]
}
}}
}}
]);
db.COLLECTION_NAME.aggregate([
{$addFields: {
"CONCATENATED_STRING": {$reduce: {
"input": "$ARRAY_OF_STRINGS_FIELD_NAME",
"initialValue": "",
"in": {
"$concat": [
"$$value",
"$$this",
", ",
]
}
}}
}}
]);
db.COLLECTION.update(
{
$expr: {$eq: [{"$type": "$YOUR_DATE_FIELD"}, "object"]},
},
{$set: {
"YOUR_DATE_FIELD": new Date(parseInt("$YOUR_DATE_FIELD.$date.$numberLong")),
}},
{
multi: true,
upsert: false,
}
);
Original issue:
Uncaught MinimongoError: Key $date must not start with '$'
db.COLLECTION_NAME.aggregate([
{$addFields: {
'SUB_DOCUMENT_FIELD_COUNT':
{$size:
{$objectToArray:
{$ifNull: [
"$YOUR_FIELD",
{}
]}
}
},
}},
]);
db.COLLECTION_NAME.aggregate([
{$addFields: {
'THIS_DOCUMENT_KEY_COUNT': {$size: {$objectToArray: "$$ROOT"}},
}},
]);
import pandas as pd
import numpy as np
from tabulate import tabulate
# 100 rows, 3 columns, random numbers
df = pd.DataFrame(
np.random.rand(100, 3),
columns=['COLUMN_1', 'COLUMN_2', 'COLUMN_3']
)
# Pretty-print it
print(tabulate(df, headers='keys', tablefmt='grid', showindex='always'))
from pymongo import MongoClient
import datetime
client = MongoClient("mongodb://YOUR_CONNECTION_STRING")
db = client.YOUR_DATABASE_NAME
collection = db.YOUR_COLLECTION_NAME
# Truncate
collection.delete_many({})
# Date treatment
date = datetime.datetime.utcnow()
at = datetime.datetime.strptime(
str(date),
"%Y-%m-%d %H:%M:%S.%f"
)
# Insertion
collection.insert_one({"YOUR_FIELD_NAME": at})
import pandas as pd
df1 = pd.DataFrame()
df2 = pd.DataFrame()
df3 = pd.concat([df1, df2])