MongoDB-使用find()获取数组中的值
我的收藏包含以下形式的文档:MongoDB-使用find()获取数组中的值,mongodb,Mongodb,我的收藏包含以下形式的文档: { 'game_name': 'football', 'scores': [4,1,2,7,6,0,5] } 我如何找到集合中所有此类对象的“分数”并按升序排序?如果我理解您的问题 db.yourcollection.aggregate([ { $unwind:"$scores" },{ $sort:{ "scores":1 } },{
{
'game_name': 'football',
'scores': [4,1,2,7,6,0,5]
}
我如何找到集合中所有此类对象的“分数”并按升序排序?如果我理解您的问题
db.yourcollection.aggregate([
{
$unwind:"$scores"
},{
$sort:{
"scores":1
}
},{
$group:{
_id:null,
scores:{$addToSet:"$scores"}
}
},{
$project:{
_id:0,
scores:1
}
}
])
结果是:
{
"result" : [
{
"scores" : [
7,
5,
4,
6,
2,
1,
0
]
}
],
"ok" : 1
}
谢谢。你能解释一下$group和$project吗?我不知道这些是什么,而$unwind对我来说是新的。@Rayu当然!我的英语不是很好,但我试图解释为数组的每个元素创建文档是相似的。我的Sql组重塑了一个文档,您可以决定要查看哪个元素,重命名和其他功能,如
$toLower
或$toUpper
等。。。医生