Skip to content

Instantly share code, notes, and snippets.

@marantz
Created September 1, 2021 01:32
Show Gist options
  • Select an option

  • Save marantz/9246a3965ba15750830f7d5f7908fe98 to your computer and use it in GitHub Desktop.

Select an option

Save marantz/9246a3965ba15750830f7d5f7908fe98 to your computer and use it in GitHub Desktop.
Document Attribute Change
[
{
"_id": 1,
"texts": [
{ "language": "english", "text": "hello" },
{ "language": "german", "text": "hallo" },
{ "language": "french", "text": "bonjour" }
]
}, …
]
[
{
"_id": 1,
"texts": {
"english": "hello",
"german": "hallo",
"french": "bonjour"
}
}, …
]
db.col.aggregate([{
"$project": {
"texts":{
"$arrayToObject":{
"$zip": {
"inputs": [
"$texts.language",
"$texts.text"
]
}
}
}}
}])
db.col.aggregate([
{ "$addFields": {
"texts": {
"$map": {
"input": "$texts",
"in": {
"k": "$$this.language",
"v": "$$this.text"
}
}
}
}},
{ "$addFields": {
"texts": { "$arrayToObject": "$texts" }
}}
])
db.col.aggregate([
{
$addFields: {
texts: {
$arrayToObject: {
$map: {
input: "$texts",
as: "text",
in: {
k: "$$text.language",
v: "$$text.text"
}
}
}
}
}
}
])
db.collection.aggregate([
{ "$group": {
"_id": {
"date": "$install_date",
"platform": { "$toLower": "$platform" }
},
"count": { "$sum": 1 }
} },
{ "$group": {
"_id": "$_id.date",
"counts": {
"$push": {
"k": "$_id.platform",
"v": "$count"
}
}
} },
{ "$addFields": {
"install_date": "$_id",
"platform": { "$arrayToObject": "$counts" }
} },
{ "$project": { "counts": 0, "_id": 0 } }
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment