mongoose - 使用 Mongoose 按名称排序根,按位置排序它们的图像
问题描述
我正在制作一个带有 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
存在或不存在,则它应该位于图像列表的末尾。有任何想法吗??
解决方案
推荐阅读
- php - 对数组中的值进行分组和计算
- java - 无法使用休眠从 oracle 表中选择数据
- java - 交叉两个不同长度的个体
- android - 为什么我会收到此“java.lang.NullPointerException: Attempt to invoke virtual method '...' on a null object reference”错误?
- tensorflow - ConvLSTM2D 数据准备
- java - 基于json的Java泛型声明
- redis - 无法在 CentOS 7 上安装最新的稳定版 Redis
- python - 具有递归的多维列表中的整数和
- c++ - 在 Cygwin 下包含包含的简单 C++ 编译错误
- html - 在没有额外空间的情况下居中 flex 子项