uı6*_*uɐp 3 java mongodb aggregation-framework
我正在尝试通过Java API在MongoDB上执行查找后对值进行排序.结果列表包含以下条目:
{
"_id": "P17-223",
"property": "P17",
"itemid": 223,
"labels": [
{
"language": "en",
"value": "Greenland"
},
{
"language": "es",
"value": "Groenlandia"
},
{
"language": "de",
"value": "Grönland"
}
]
Run Code Online (Sandbox Code Playgroud)
}
我想按数组标签的第一个条目排序:
DBCursor cursor = getCollection().find(query);
BasicDBObject orderBy = new BasicDBObject("labels[0].value", 1);
cursor.sort(orderBy);
Run Code Online (Sandbox Code Playgroud)
此代码不对游标值进行排序.你能帮助我吗?
你有没有尝试过
BasicDBObject orderBy = new BasicDBObject("labels.0.value", 1);
Run Code Online (Sandbox Code Playgroud)
这并不明显,但是MongoDB文档却没有提到它.使用$符号匹配第一个项目,但指定数组元素编号似乎有效.如果有人有更好的文档描述行为,请回复链接.
从文档中
更新阵列中的文档
The positional $ operator facilitates updates to arrays that contain embedded
documents. Use the positional $ operator to access the fields in the embedded
documents with the dot notation on the $ operator.
db.collection.update( { <query selector> }, { <update operator>: { "array.$.field" : value } } )
Run Code Online (Sandbox Code Playgroud)
文档在这里
我在查找How to sort MongoDB (in Meteor) by first item of array时发现了这个问题/答案......
我用它来查找Schedules集合中的所有文档active,然后按days数组中的第一天升序排序。
Schedules.find(
{
active: true,
},
{
sort: { 'days.0': 1 },
},
)
Run Code Online (Sandbox Code Playgroud)
Schedule具有星期一、星期三、星期五时间表的示例集合文档如下所示:
{
_id: 9dh3ld0d7d0d,
userId: UJD9dKd8edo,
active: true,
days: [1, 3, 5],
}
Run Code Online (Sandbox Code Playgroud)
实际上,您无法按 MongoDB 文档中数组的特定索引进行“排序”。ct 如果您确实必须这样做,那么您需要聚合框架来“提取”要排序的元素。
我知道列表形式实际上已被弃用,因此此代码仅用于演示。实际上将管道定义为单独的变量,并将它们作为参数提供给聚合:
BasicDBList pipeline = new BasicDBList();
list.add(new BasicDBObject("$unwind","$labels"));
list.add(new BasicDBObject("$group",
new BasicDBObject("_id","$_id")
.append("property", new BasicDBObject("$first","$property"))
.append("itemid", new BasicDBObject("$first","$itemid"))
.append("labels", new BasicDBObject("$push","$labels"))
.append("maxLabel", new BasicDBObject("$max", "$labels.value"))
));
list.add(new BasicDBObject("$sort", new BasicDBObject("maxLabel",1)));
System.out.println(pipeline);
Run Code Online (Sandbox Code Playgroud)
这将为您提供 JSON 形式的序列化版本:
db.collection.aggregate([
{ "$unwind" : "$labels" },
{ "$group": {
"_id": "$_id",
"property": { "$first" : "$property" },
"itemid": { "$first" : "$itemid" },
"labels": { "$push" : "$labels" },
"maxLabel": { "$max" : "$labels.value"}
}},
{ "$sort" : { "maxLabel" : 1} }
])
Run Code Online (Sandbox Code Playgroud)
更好地应用在您的代码中:
collection.aggregate(unwind,group,sort);
Run Code Online (Sandbox Code Playgroud)
这些是单独声明的。
| 归档时间: |
|
| 查看次数: |
1882 次 |
| 最近记录: |