I have collection
[{
"_id" : "5c9a69ef42c82b0197a2ffb8",
"key" : "a",
"language" : "en",
"version" : "0.1",
},
{
"_id" : "5c9a69ef42c82b0197a2ffb8",
"key" : "b",
"language" : "en",
"version" : "0.1",
},
{
"_id" : "5c9a69ef42c82b0197a2ffb8",
"key" : "b",
"language" : "en",
"version" : "0.2",
},
{
"_id" : "5c9a69ef42c82b0197a2ffb8",
"key" : "c",
"language" : "en",
"version" : "0.2",
}]
so I have 2 queries that getting version 0.1 and version 0.2, And I need to find the difference between them?
I need to get all of translation 0.2 that does not exist in translation 0.1.
Here is a solution to get all translations key
that has a 0.2 version but not a 0.1 version.
$sort
the translations by versions.$group
, I get all the versions for a given key
.$match
remove all the key that have a version 0.1 $replaceRoot
keep only the last version for a key
db.collection.aggregate([
{
"$sort": {
"version": 1
}
},
{
$group: {
"_id": "$key",
"items": {
"$push": "$$ROOT"
}
}
},
{
"$match": {
"items.version": {
"$ne": "0.1"
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$last": "$items"
}
}
}
])
try it here
If you remove the $match
, you can also retrieve the last version available for each translation.
db.collection.aggregate([
{
"$sort": {
"version": 1
}
},
{
"$group": {
"_id": "$key",
"items": {
"$push": "$$ROOT"
}
}
},
{
"$replaceRoot": {
"newRoot": {
"$last": "$items"
}
}
}
])
try it here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.