A
A
AlexZeDim2017-04-16 12:08:01
JavaScript
AlexZeDim, 2017-04-16 12:08:01

MongoDB + mongoose + nodejs data aggregation and math operations?

Hello everyone, I 'm learning nodejs and mongodb , there are no problems with the first one, but with the last one . in mongodb (through mongoose, but in search of solutions to my problem, any options / drivers are possible at all) Where is the problem: sometimes I have to output very "exotic arrays of data", from my mongo database for example: my documents (and in imported JSON ) have fields "total_price" (which is like the check amount) and "quantity"

, but there is no "price per piece", and the question is how to make/display this field.
or
you need to display certain documents by title and "timestamp " and then sum the "quantity" field of the output array. The question is, can Mongodb
do this on its own? Not by outside means. For example, according to the official documentation, to solve the first problem, there is nothing easier than calling through the mongo shell:

db.имя_коллекции.aggregate(
    [
        { $project: { name: 1, Price: { $divide: [ "total_price", "quantity" ] } } }
    ]
);

but the problem is that on the site with the documentation for mongoose, there is practically no word about aggregation and arithmetic operations. (to be honest, I didn’t even find a search there, I had to search everything through site: mongoosejs, com)
So what’s the matter, I want too much from mongoose and he just doesn’t know how to aggregate and mathematical operations? (for example, in Math.floor and Math.ceil mongo does not yet know how) Or, am I a fool and just looking in the wrong direction and do not understand the principle of using drivers? Where, then, is the very place where you need to insert this wonderful mongo-native-shell-code into Mongoose (something like with SQL , once and done)? Or is it necessary to use Mongo Native Driver for this purpose?
Or is it even worse, and when you have to call every time
.find({ условие:здесь }, null, {sort: 'отсортируюкакнибудь'},function (err, res) 
   получать выборку от базы в JSON и оперировать уже с ней/res (мат.операции и т.д.), 
а затем запихивать все назад в базу через .insert;

Surely, I'm not the only one with such a question, and certainly someone else uses the nodejs + mongo bundle for similar queries to the database. Could you be so kind as to show part of your code that performs such operations, which will reduce my number of useless questions in the future by 99%
//PS Honestly, sometimes this raises the question: 'Wouldn't it be easier do everything on mySQL ?'
//PPS Since the Stackoverflow community in Russian resembles a cemetery, I initially leave my question here, but if anything, suddenly, then here is a link to my same question, there

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
emp1re, 2017-04-16
@AlexZeDim

mongoose is odm, no one said that you can't use aggregation on schema mongoose.
I myself use mongoose only for data validation, and this is also already implemented in the native driver.
about the code something like this

user.aggregate([...])
где user = new mongoose.Schema('User')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question