[英]How to filter document by a value in the dictionary in arangodb?
I have following structure in arangodb collection named clientShadow
.我在名为
clientShadow
的 arangodb 集合中有以下结构。
{
"_class": "com.syncservice.document.ClientShadow",
"sessions": {
"session-id-2": {
"workspaceId": "workspace-id-1",
"message": {
"lastSynced": 0,
"lastAvailable": 1674630705773
}
},
"session-id-1": {
"workspaceId": "workspace-id-1",
"message": {
"lastSynced": 0,
"lastAvailable": 1674630705773
}
},
"session-id-0": {
"workspaceId": "workspace-id-1",
"message": {
"lastSynced": 0,
"lastAvailable": 1674630705773
}
}
},
"synced": true
}
Here sessions
is a map/dictionary of session_id as string and session object as value.这里的
sessions
是 session_id 作为字符串和 session object 作为值的映射/字典。
I want to fetch all the sessions from collection where session's lastSynced
and lastAvailable
aren't same.我想从会话的
lastSynced
和lastAvailable
不相同的集合中获取所有会话。
I tried following query我尝试了以下查询
FOR doc IN clientShadow
FOR session IN doc['sessions']
FILTER session.message.lastSynced != session.message.lastAvailable
RETURN {'session': session}
But I found out that FOR IN works with collections and gives me following error但我发现 FOR IN 与 collections 一起使用并给了我以下错误
Query: AQL: collection or array expected as operand to FOR loop;
查询:AQL:集合或数组预期作为 FOR 循环的操作数; you provided a value of type 'object' (while executing)
您提供了“对象”类型的值(在执行时)
To retain the original data and query structure, don't use ATTRIBUTES
, use VALUES
:要保留原始数据和查询结构,请不要使用
ATTRIBUTES
,使用VALUES
:
FOR doc IN clientShadow
FOR session IN VALUES(doc['sessions'])
FILTER session.message.lastSynced != session.message.lastAvailable
RETURN {'session': session}
If you cannot change the data structure, you can use ATTRIBUTES
to access the sessions as array:如果您无法更改数据结构,则可以使用
ATTRIBUTES
以数组形式访问会话:
Edit: Code as fixed by Kshiti Kshitij Dhakal (Attributes returns a list of the names of the attributes)编辑:由 Kshiti Kshitij Dhakal 修复的代码(属性返回属性名称列表)
FOR doc IN clientShadow
FOR session IN ATTRIBUTES(doc['sessions'])
FILTER
doc.sessions[session].message.lastSynced
!= doc.sessions[session].message.lastAvailable
RETURN {'session': doc.sessions[session]}
Old (wrong) suggestion:旧的(错误的)建议:
FOR doc IN clientShadow
FOR session IN ATTRIBUTES(doc['sessions'])
FILTER session.message.lastSynced != session.message.lastAvailable
RETURN {'session': session}
If you can change the data structure, don't use an object for sessions, but a list:如果可以更改数据结构,请不要为会话使用 object,而是使用列表:
{
"_class": "com.syncservice.document.ClientShadow",
"sessions": [
{
"sessionId": "session-id-2",
"workspaceId": "workspace-id-1",
"message": {
"lastSynced": 0,
"lastAvailable": 1674630705773
}
},
...
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.