Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do some math in a query?#493

Unanswered
pbhuter asked this question inQ&A
Nov 15, 2022· 1 comments· 2 replies
Discussion options

Is it possible to evaluate multiple fields in a query and do math with the values in those fields to compare to a given value? For example, this is what I tried (unsuccessfully):

db.search(math.sqrt(math.pow((A.B['a'] - a), 2) + math.pow((A.B['b'] - b), 2) + math.pow((A.B['c'] - c), 2)) < err)

As I said, the above does not work, but I am hoping there is something similar that I can do. Specifically, I am trying to calculate the RMSE of three points and comparing that value to some error threshold.

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

Hey@pbhuter, I think you could use themap operator:

defrms(doc):returnmath.sqrt(math.pow(doc['a']-a,2)+math.pow(doc['b']-b,2)+math.pow(doc['c']-c,2)    )print(db.search(A.B.map(rms)<err))

That way you can also test that therms function works correctly e.g. in unit tests.

Does that work for you?

You must be logged in to vote
2 replies
@pbhuter
Comment options

Hey@msiemens -

Do you by any chance know how I should structure my search to access the 'a', 'b', 'c' in the following:

{"_default": {"1": {"Pc": "2429546524130460032", "Pf": "2429519276857919232", "Points": [{"P": "2428316170619108992", "a": "0.0690932185744956", "b": "2.6355498567408557", "c": "0.4369495787854096"}...

Where there are a total of 10 Points in several thousand objects (the "1" at the beginning is the first object). I am able to access "Pc" and "Pf", but if I try:

db.search(Point.Points['a'] == '0.0690932185744956'
I get an empty set.

Thoughts?

@msiemens
Comment options

I think you could use theany query operator:

print(db.search(Point.Points.any(Point.a=='0.0690932185744956')))

Of course, you can combine this with the RMS query:

defrms(doc):returnmath.sqrt(math.pow(doc['a'],2)+math.pow(doc['b'],2)+math.pow(doc['c'],2)    )db=TinyDB('test.json')db.truncate()db.insert({"Pc":2429546524130460032,"Pf":2429519276857919232,"Points": [{"P":2428316170619108992,"a":0.0690932185744956,"b":2.6355498567408557,"c":0.4369495787854096}]})print(db.search(Query().Points.any(Query().map(rms)<3)))
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@pbhuter@msiemens

[8]ページ先頭

©2009-2025 Movatter.jp