MongoDB noob在这里......
好的,我有一个学生集合,每个都有一个如下所示的记录....我想按照降序排序'类型':'家庭作业'分数.
那个咒语在mongo shell上是什么样的?
> db.students.find({'_id': 1}).pretty()
{
"_id" : 1,
"name" : "Aurelia Menendez",
"scores" : [
{
"type" : "exam",
"score" : 60.06045071030959
},
{
"type" : "quiz",
"score" : 52.79790691903873
},
{
"type" : "homework",
"score" : 71.76133439165544
},
{
"type" : "homework",
"score" : 34.85718117893772
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这个咒语....
doc = db.students.find()
for (_id,score) in doc.scores:
print _id,score
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我的文档结构如下:
{
map: 'A',
points: [
{
type: 'type1',
distanceToSpawn: 110
},
{
type: 'type4',
distanceToSpawn: 40
},
{
type: 'type6',
distanceToSpawn: 30
}
]
},
{
map: 'B',
points: [
{
type: 'type1',
distanceToSpawn: 100
},
{
type: 'type2',
distanceToSpawn: 60
},
{
type: 'type3',
distanceToSpawn: 25
}
]
},
{
map: 'C',
points: [
{
type: 'type2',
distanceToSpawn: 90
},
{
type: 'type3',
distanceToSpawn: 1
},
{
type: 'type6',
distanceToSpawn: 76
}
]
}
Run Code Online (Sandbox Code Playgroud)
我希望得到所有具有type1按distanceToSpawn …
该.sort()功能似乎对我根本不起作用,它不会对我所做的任何事情进行排序。
我通过 Handlebars {{Book}} 显示输出
router.get("/", (req, res) => {
Book.find({ _id: req.params.id })
.sort({ 'chapters.orderIndex': 1 }) //wont sort
.then(books => {
res.render("books/index", {
books: books
})
});
});
Run Code Online (Sandbox Code Playgroud)
我也试过:
.sort({ 'Book.chapters.orderIndex': 1 })
.sort({ 'Book.date': 1 })
.sort({ 'date': 1 }) //field from Book
.sort({ date: 1 })
Run Code Online (Sandbox Code Playgroud)
也尝试过asc/desc而不是使用1/-1
知道为什么.sort()不起作用吗?