我正在制作一个带有react,graphql,apollo和mongoose的应用程序,我想按名称(在这种情况下没有问题)以及按位置排列自己的一组图像来排序我的数据。我有这样的东西(在伪json中):
{data:
{
name: "Name 3"
Images:[
{
name: "Image 3.22"
position: 2
},
{
name: "Image 3.91"
position: 1
},
{
name: "Image 3.73"
}
},{
{
name: "Name 2"
Images:[
{
name: "Image 2.53"
position: 3
},
{
name: "Image 2.61"
position: 1
},
{
name: "Image 2.32"
position: 2
}
}
结果必须是:
{data:
{
name: "Name 2",
Images:[
{
name: "Image 2.61"
position: 1
},
{
name: "Image 2.32"
position: 2
},
{
name: "Image 2.53"
position: 3
}]
},{
name: "Name 3"
Images:[
{
name: "Image 3.91"
position: 1
},
{
name: "Image 3.22"
position: 2
},
{
name: "Image 3.73"
}]
}
}
我使用return ArticleModel.find(filtro).limit(20).sort({name: "asc", "Images.position": "desc"}).select(projection).exec();
,但这是错误的。如果该位置是null
或不存在,那么它应该在图像列表的末尾。
有什么想法吗?