【问题标题】:Sort data by fields from different collections using mongoose Node Js使用 mongoose Node Js 按来自不同集合的字段对数据进行排序
【发布时间】:2016-11-20 23:33:15
【问题描述】:

我有两个收藏:

Person schema 
{
  _id     : Number,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
}


Story schema
{
  _creator : { type: Number, ref: 'Person' },
  title    : String,
  fans     : [{ type: Number, ref: 'Person' }]
}

我正在使用以下查询从“Story”集合中获取数据,但我希望数据应按“Person”集合的“name”字段排序:

Story.find({"title" : "education"}).populate('fans').populate('_creator').exec()

如何按“person”集合的“name”字段对数据进行排序?

【问题讨论】:

    标签: node.js mongoose mongoose-populate


    【解决方案1】:

    试试:

    Story.find({"title" : "education"}).populate({path : 'fans', options :{sort : { name : 1 }}}).populate({path :'_creator' , options : {sort : { name : 1 }}}).exec();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多