简体   繁体   English

在MongoDB中排序

[英]Sorting in MongoDB

I don't have any idea how to get ordered list from Mongo data. 我不知道如何从Mongo数据中获取有序列表。 I need to return .find() result from mongo collection, in kind of this way: 我需要以这种方式从mongo集合return .find()结果:

For example! 例如! At first, I have One collection for cars with 10 documents: 首先,我有一个包含10个文档的汽车集合:

//doc structure:
{
  _id: 10,
  name: name,
  type: [suv, sedan, sport],
  driver: [object Driver]
  pubDate: new Date()
}

And for second, I have Two col. 第二,我有两个上校。 with two types of drivers: 有两种类型的驱动程序:

//doc structure:
{
  _id: 20,
  name: name,
  type: stupid 'or' clever
}

And now I want to return list in this order, for example: 现在,我想按此顺序返回列表,例如:

  1. name: Ford Focus, driver: {name: Nick, type: clever }, pubDate: 20:00 ; 名称:Ford Focus,驱动程序:{名称:尼克,类型: 聪明 },发布日期: 20 : 00 ;
  2. name: Nissan GTR, driver: {name: Andrew, type: clever }, pubDate: 19:00 ; 名称:日产GTR,驱动程序:{名称:安德鲁,类型: 聪明 },发布日期: 1900
  3. name: Jaguar xKR, driver: {name: John, type: clever }, pubDate: 15:00 ; 名称:Jaguar xKR,驱动程序:{名称:John,类型: 聪明 },发布日期: 15 : 00 ;
  4. name: Honda Accord, driver: {name: Petr, type: clever }, pubDate: 14:00 名称:Honda Accord,驾驶员:{名称:Petr,类型: 聪明 },发布日期: 14 : 00
  5. name: Nissan GTR, driver: {name: Andrew, type: clever }, pubDate: 13:00 ; 名称:日产GTR,驱动程序:{名称:安德鲁,类型: 聪明 },发布日期: 13 : 00 ;
  6. name: Tesla, driver: {name: Jorge, type: stupid }, pubDate: 20:00 ; 名称:Tesla,驱动程序:{名称:Jorge,类型: 愚蠢 },发布日期: 20 : 00 ;
  7. name: Audi q7, driver: {name: Peggy, type: stupid }, pubDate: 19:00 ; 名称:Audi q7,驾驶员:{名称:Peggy,类型: 愚蠢的 },发布日期: 1900
  8. name: BMW 325, driver: {name: Minnie, type: stupid }, pubDate: 18:00 ; 名称:宝马325,驾驶员:{名称:米妮,类型: 愚蠢 },发布日期: 1800
  9. name: CADILLAC, driver: {name: Timothy, type: stupid }, pubDate: 16:00 ; 名称:CADILLAC,驱动程序:{名称:蒂莫西,类型: 愚蠢 },发布日期: 16 : 00 ;
  10. name: SAAB, driver: {name: Julia, type: stupid }, pubDate: 15:00 ; 名称:SAAB,驱动程序:{名称:朱莉娅,类型: 愚蠢 },发布日期: 15 : 00 ;

Nowadays, I have one solution, but It's not correct, and it's not work with infinite scroll. 如今,我有一个解决方案,但这是不正确的,并且不适用于无限滚动。 Meteor example: 流星的例子:

// declare collections
Cars = Mongo.collection('cars', {
  name: name,
  type: [suv, sedan, sport],
  driver: [object Driver]
  pubDate: new Date()
}); 

Driver = Mongo.collection('cars', {
  name: name,
  lastName: lastName,
  type: stupid // or clever
}); 



// declare helpers for client:
Template.carList.helpers({
  'clever': function() {
     return Cars.find({driver.type: 'clever'}, {sort: {pubDate: -1});
     // returned only clever
   },
  'stupid': function() {
     return Cars.find({driver.type: 'stupid'}, {sort: {pubDate: -1});
     // returned only stupid
  }
 }):

And HTML with blaze: 和HTML大放异彩:

<template name='carList'>
     ...
      {{#each clever}}
        ...
      {{/each}}
      {{#each stupid}}
        ...
      {{/each}}
</template>

It's work, but it's not flexible way, because if i need to set some new parameters for sorting, I'll get many problems with it. 这是可行的,但是它不是灵活的方法,因为如果我需要设置一些新的参数进行排序,则会遇到很多问题。 And this example with one Drivers collection, but what if types of users will be in separate collections, like Stupid & Clever? 这个示例只有一个Drivers集合,但是如果用户类型位于单独的集合中,如Stupid&Clever,该怎么办?

I think that there must be a more elegant way for aggregate this. 我认为必须有一种更优雅的方法来汇总这一点。

Which? 哪一个? Thank you! 谢谢!

How about passing the sort list to the helper and using that to build the sort? 如何将排序列表传递给帮助程序并使用它来构建排序?

Super-simple off-the-top-of-my-head example: 超级简单的例子:

Template.carList.helpers({
  clever: function(sortField) {
    options = {};
    if (sortField) {
      options.sort = {sortField: -1};
    }
    return Cars.find({driver.type: 'clever'}, options);
  }
});

And in the HTML: 在HTML中:

<template name='carList'>
     ...
      {{#each clever "name"}}
        ...
      {{/each}}
      {{#each stupid}}
        ...
      {{/each}}
</template>

You could get a little more complex and pass in key/value pairs, which Spacebars supports via Spacebars.kw (read here and here ). 您可能会变得更加复杂,并传递键/值对,Spacebars通过Spacebars.kw支持键/值对( 在此处此处阅读)。 You would essentially pass in the arguments like name=-1 pubDate=1 , then in your helper parse out the resulting hash that is passed in and add them to your options. 从本质上讲,您将传入name=-1 pubDate=1类的参数,然后在助手中解析传入的结果哈希并将它们添加到您的选项中。 You may need to tinker with how it works, but that should get you started at least. 您可能需要修改其工作原理,但这至少应该可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM