从MongoDB docs,我知道您可以使用以下内容指定要返回的字段:
db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 0 } )
您可以使用sort()
db.bios.find().sort( { name: 1 } )
但是,SO上的several solutions建议采用以下格式:
DrawingHistory.find({}, {sort: {time: -1}});
Aphorisms.find({}, {sort: {date_created: -1}});
所以我尝试了这些,但都没有效果:
return Questions.find({}, {text: 1, options: 1}).sort({order: 1}); // Returns nothing
return Questions.find({}, {text: 1, options: 1}, {sort: {order: 1}}); // Doesn't work
return Questions.find({}, {sort: {order: 1}}, {text: 1, options: 1}); // Only first filter gets applied
示例文档/行:
{
"options": [
{
"answer": "Answer I",
"value": "A"
},
{
"answer": "Answer II",
"value": "B"
},
{
"answer": "Answer III",
"value": "C"
},
{
"answer": "Answer IV",
"value": "D"
}
],
"order": 1,
"text": "Question A"
}
那么,在Meteor中同时指定字段以返回 AND 排序的正确方法是什么?
答案 0 :(得分:7)
您尝试使用mongo shell API查询,这与Meteor find API略有不同。在Meteor中,选项都在第二个参数中传递:
Questions.find({}, {
fields: {
text: 1,
options: 1
},
sort: {
order: 1
}
});