Answer the question
In order to leave comments, you need to log in
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" ] } } }
]
);
.find({ условие:здесь }, null, {sort: 'отсортируюкакнибудь'},function (err, res)
получать выборку от базы в JSON и оперировать уже с ней/res (мат.операции и т.д.),
а затем запихивать все назад в базу через .insert;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question