How do you perform basic arithmetic in a MongoDB query?
-
In SQL, you can select many fields and perform basic arithmetic on them, by creating a query such as "SELECT ((field1 + field2)/2) as relevance, title, author from..."
-
Answer:
You can actually use a javascript expression to filter your query. It's just not advisable where performance matters: See docs here: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D
Jonathan Hoffman at Quora Visit the source
Other answers
You cannot. You should return the data and do the arithmetic in the client.
Scott Hernandez
It's very easy: just download SlamData, an open source project that lets you run SQL on MongoDB, and then write: "SELECT ((field1 + field2)/2) as relevance, title, author from..." SlamData compiles the SQL query to an efficient MongoDB query plan, which in the case of this simple query, will be an aggregation pipeline command. More complex cases, such as JOINs or advanced WHERE clauses may be compiled down to Javascript MapReduce jobs, which is OK for a reporting use case but not for a real-time use case inside an app.
John A. De Goes
Related Q & A:
- How to write a complex query with doctrine?Best solution by Stack Overflow
- How do I perform this ActiveRecord query?Best solution by Stack Overflow
- How to convert a SQL query into hibernate criteria?Best solution by Stack Overflow
- How to make a MySql query faster?Best solution by Stack Overflow
- How does a basic RN become a Surgical Nurse?Best solution by innerbody.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.