Node.js Mongoose查询对主文档进行排序&;按特定字段列出的子文档

Node.js Mongoose查询对主文档进行排序&;按特定字段列出的子文档,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有3份文件 const designTypeSchema = new mongoose.Schema ({ name : { type : String, trim: true, required : true, }, image : { type : String, trim: true, required : true, }, status: {

我有3份文件

 const designTypeSchema = new mongoose.Schema ({
    name : {
        type : String,
        trim: true,
        required : true,
    },
    image : {
        type : String,
        trim: true,
        required : true,
    },
    status: { 
        type : Boolean,
        default: true
    } 
}
);

const tagTypeSchema = new mongoose.Schema ({
    name : {
        type : String,
        required : true,
    },
    design_type :
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'DesignType',
    }
    ,
    order : {  //need to apply sorting for this field
        type : Number,
        trim: true,
    },
    status: { 
        type : Boolean,
        default: true
    }  } );


const TagSchema = new mongoose.Schema ({
    name : {
        type : String,
        required : true,
    },
    tag_type : {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'TagType',
    image : {
        type : String,
        trim: true,
    },
    order : { //need to apply sorting for this field
        type : Number,
    },
    status: { 
        type : Boolean,
        default: true
    } 
}, 
);
我需要对两个字段应用排序,TagType.order和Tag.order

TagType将被视为主文档,所以首先TagType应该按顺序字段列出,然后Tag将是子文档,它应该按Tag.order字段列出

我尝试了以下查询:

TagType.aggregate(
      [
        {
          $lookup: {
            from: "tags",
            localField: "_id",
            foreignField: "tag_type",
            as: "tags",
          },
      },
      {
          $lookup: {
              from: "designtypes",
              localField: "design_type",
              foreignField: "_id",
              as: "designtype",
          }
        },
        {
          $project:
          {
            _id: 1,
            name: 1,
            tag_type: 1,
            order: 1,
            "designtype.name":1,
            "tags._id":1,
            "tags.name":1,
            "tags.image":1,
            "tags.order":1,
            totalTags: {$size: "$tags"},
          }
        },
        {
          $sort : { 
            "order": -1,
            'tags.order' : -1
          } 
        },
      ]
    ).exec(function(err, results) {
      if (err) {
        throw err;
      }
      res.status(200).json({
          message: 'success',
          total: results.length,
          response: {
              data: results
          }
      })
    });
使用上面的查询,我得到排序结果,但它只对主文档应用排序,而不对子文档(标记)应用排序。我在得到期望结果的查询中缺少什么

我的问题与此相关:。但这个问题并没有得到想要的答案。所以我贴了一张新的


任何帮助都将不胜感激。谢谢

目前无法直接在数组对象内部进行排序

你可以做两个选择之一

  • 如果您是从查找中获取数据,那么使用它将允许在匹配文档中使用
    $sort
    管道
  • $REWIND数组=>$sort it=>再次$group它到数组中
这里您使用的是
$lookup
,而不是简单的查找,您可以使用“$lookup with pipeline”



第二种可能的解决方案:

当前无法直接在数组对象内部进行排序

你可以做两个选择之一

  • 如果您是从查找中获取数据,那么使用它将允许在匹配文档中使用
    $sort
    管道
  • $REWIND数组=>$sort it=>再次$group它到数组中
这里您使用的是
$lookup
,而不是简单的查找,您可以使用“$lookup with pipeline”



第二种可能的解决方案:

您可以使用
展开
添加另一种可能的解决方案吗?我已经为那个旧问题添加了一个答案,并在这个答案中添加了您可以查看的链接。也在这个答案中添加了您的解决方案。您可以看看这个问题吗?请检查一下你是否能帮助我!谢谢。请使用
展开
添加另一个可能的解决方案?我已经为那个旧问题添加了一个答案,并在这个答案中添加了链接,您可以查看。也在这个答案中添加了您的解决方案。您能看一下这个问题吗?请检查一下你是否能帮助我!谢谢
  {
    $lookup: {
      from: "tags",
      as: "tags",
      let: { id: "$_id" },
      pipeline: [
        {
          $match: {
            $expr: { $eq: ["$$id", "$tag_type"] }
          }
        },
        {
          $sort: { order: -1 }
        }
      ]
    }
  },