Last active
September 2, 2020 05:36
-
-
Save hochun836/497c2a5552d19e2af5a72b0dc20a74b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// data | |
{ | |
"_id" : ObjectId("5f4f03709f80fb9179d5136e"), | |
"data1" : 1.0, | |
"data2" : 2.0, | |
"data3" : 3.0, | |
"data4" : 4.0, | |
"data5" : 5.0, | |
"other" : { | |
"data6" : 6.0, | |
"data7" : 7.0, | |
"data8" : 8.0, | |
"data9" : 9.0, | |
"data10" : 10.0 | |
} | |
} | |
// way 1: $project | |
db.getCollection('test').aggregate([ | |
{ $project: { | |
_id: 0, | |
data1: 1, | |
data2: 1, | |
data3: 1, | |
data4: 1, | |
data5: 1, | |
data6: '$other.data6', | |
data7: '$other.data7', | |
data8: '$other.data8', | |
data9: '$other.data9', | |
data10: '$other.data10', | |
}} | |
]) | |
// way 2: $replaceRoot & $mergeObjects & $unset($project) | |
db.getCollection('test').aggregate([ | |
{ $replaceRoot: { newRoot: { $mergeObjects: ['$$ROOT', '$other'] } }}, | |
{ $project: { other: 0 } }, | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment