英文:
Mongo SORT with specific entry from list of attribute
问题
我正在使用Java处理Mongo聚合/筛选/排序和限制。
我有以下模型对象。CustomPOJO
是我的数据库集合。
@Document(collection = "CustomPOJO")
public class CustomPOJO {
public List<KeyValueObject> valueList;
public String attribute1;
public String attribute2;
public CustomObject attribute3;
}
public class CustomObject {
public String attribute4;
public String attribute5;
}
public class KeyValueObject {
public String name;
public String value;
}
现在我在我的数据库中有以下记录条目:
记录1:
{
"valueList": [{
"name": "field1",
"value": "value1"
}, {
"name": "field2",
"value": "value2"
}, {
"name": "field3",
"value": "value3"
}],
"attribute1": "attribute1",
"attribute2": "attribute2",
"attribute3": {
"attribute4": "attribute4",
"attribute5": "attribute5"
}
}
记录2:
{
"valueList": [{
"name": "field1",
"value": "value4"
}, {
"name": "field2",
"value": "value5"
}, {
"name": "field3",
"value": "value6"
}],
"attribute1": "attribute11",
"attribute2": "attribute22",
"attribute3": {
"attribute4": "attribute44",
"attribute5": "attribute55"
}
}
我可以使用 sort(DESC, "attribute1")
或 sort(DESC, "attribute3.attribute4")
或 sort(DESC, "attribute2")
在所有字段上进行排序。但是我无法对 valueList
进行排序。我希望根据 valueList
内的特定 KeyValue
条目进行排序。
例如,记录1的 valueList
包含条目 field1
,所以数据库中的所有记录应该根据其中的 field1 和其内部的 value 进行排序。
总之,我想要在列表属性内部的字段上进行排序。
非常感谢您的帮助。
英文:
I am working with Mongo aggregation/filter/sorting and limit with JAVA.
I have below model objects. CustomPOJO
is my database collection.
@Document(collation = "CustomPOJO")
public class CustomPOJO {
public List<KeyValueObject> valueList;
public String attribute1;
public String attribute2;
public CustomObject attribute3;
}
public class CustomObject {
public String attribute4;
public String attribute5;
}
public class KeyValueObject {
public String name;
public String value;
}
Now I have entries in my DB with below records
Record1:
{
"valueList": [{
"name": "field1",
"value": "value1"
}, {
"name": "field2",
"value": "value2"
}, {
"name": "field3",
"value": "value3"
}],
"attribute1": "attribute1",
"attribute2": "attribute2",
"attribute3": {
"attribute4": "attribute4",
"attribute5": "attribute5"
}
}
Record2:
{
"valueList": [{
"name": "field1",
"value": "value4"
}, {
"name": "field2",
"value": "value5"
}, {
"name": "field3",
"value": "value6"
}],
"attribute1": "attribute11",
"attribute2": "attribute22",
"attribute3": {
"attribute4": "attribute44",
"attribute5": "attribute55"
}
}
I am able to do sort on all fields using sort(DESC,"attribute1")
or sort(DESC,"attribute3.attribute4")
or sort(DESC,"attribute2")
. But i am unable to sort on valueList
. I want to do sort according to specific KeyValue
entry inside of valueList
.
For example record1 have valueList with entry field1
so all records in DB should sort according field1 and value inside it.
In general i want to sort on field inside of list attribute.
Any help will be highly appreciate.
答案1
得分: 1
无法在原地使用数组的键值对进行排序。您可以使用聚合框架来实现。
将与键匹配的条目投影出来,然后按值进行排序,并从文档中移除额外的字段。
类似这样:
db.collection.aggregate(
[
{
"$set": {
"sortdoc": {
"$arrayElemAt": [
{
"$filter": {
"input": "$valueList",
"cond": { "$eq": ["$$this.name", "field1"] }
}
},
0
]
}
}
},
{ "$sort": { "sortdoc.value": -1 } },
{ "$project": { "sortdoc": 0 } }
]
)
您还可以将valueList
建模为对象,就像您为属性做的那样。使用Document
类或HashMap
代替值的列表,并使用sort(DESC, "document.field1")
。
@Document(collation = "CustomPOJO")
public class CustomPOJO {
public Document document;
public String attribute1;
public String attribute2;
public CustomObject attribute3;
}
文档:
{
"document": {
"field1": "value1",
"field2": "value2"
},
"attribute1": "attribute1",
"attribute2": "attribute2",
"attribute3": {
"attribute4": "attribute4",
"attribute5": "attribute5"
}
}
英文:
You can't sort using array's key value pair in place. You could use aggregation framework.
Project the document matching key entry followed by sort on the value and remove the additional field from the document.
Something like
db.collection.aggregate(
[
{"$set":{
"sortdoc":{
"$arrayElemAt":[
{
"$filter":{
"input":"$valueList",
"cond":{"$eq":["$$this.name","field1"]}
}
},0]}
}},
{"$sort":{"sortdoc.value":-1}},
{"$project":{"sortdoc":0}}
])
https://mongoplayground.net/p/gzLQm8m2wQW
You could also model valueList as object just the way you have for attributes. Use Document class or HashMap instead of List of values and use sort(DESC,"document.field1")
@Document(collation = "CustomPOJO")
public class CustomPOJO {
public Document document;
public String attribute1;
public String attribute2;
public CustomObject attribute3;
}
Document
{
"document": {
"field1": "value1",
"field2": "value2"
},
"attribute1": "attribute1",
"attribute2": "attribute2",
"attribute3": {
"attribute4": "attribute4",
"attribute5": "attribute5"
}
}
答案2
得分: 0
你无法直接对数组内部字段进行排序。通常我会使用 unwind
操作对数组展开,然后进行排序。
也许这对你有所帮助。
db.collection.aggregate([
{
"$unwind": "$valueList"
},
{
"$sort": {
"valueList.name": -1
}
}
])
英文:
You cannot sort documents on fields inside of Arrays directly. Generally what i do is unwind
the array and then perform sort.
May be this is something that may help you.
db.collection.aggregate([
{
"$unwind": "$valueList"
},
{
"$sort": {
"valueList.name": -1
}
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论