Mongoose how to implement statistical query, correlated query

Source: Internet
Author: User
Tags api manual

[question]mongoose How to implement statistical query, correlated queryPosted 4 year ago author a272121742 13,025 views

Recently put forward a demand for business, request to do statistics, we design the document set, the statistics may cross the document will be more, want to ask, mongoose can achieve statistical query and related query? For example, the student Document object has a subject Document object, subject Document object has a score genus, and the results of all learners are queried. My personal thinking is the general solution, the first query all students, then query all scores, and then traverse to do cumulative statistics

Studentdao.Find().Select(' _id ').Exec(function(Err,Stus){ Subjectdao.Find().where(' student_id ').Inch(Stus).Exec(function(Err,Subs){ VarSum= 0; for (var I =  0; I <  Subs.; I++) { sum +=< Span class= "PLN" > Subs[i].; } Console.sum});             

Thought that as long as the relevant query and statistical query provided, such code will become simplified, do not know familiar with mongoose or did MongoDB development you have met, really solve, thank you!

6 replies.Xiaojue 1/F • 4 years ago

I've met. of the statistical query. Later, the solution is to re-add a total number of fields after the library, each update to the field + = operation. It takes a total number to take the total. Speed is also the fastest.

Small application with MapReduce bar. But the efficiency is very low. Or do the increment better. No matter what the database--.

ym1623 2/F • 3 years ago

##### #mongoose The statistical method of the API manual:

Kitten.where(' Color ',  ' black ' ). count (function  err, Count) { if  ( Err  return HandleError err Console.log ( ' There is%d black kittens '  Count); })            /span>                

Address: Http://mongoosejs.com/docs/api.html#query_Query-count MongoDB is a file-based database, and there is no advantage in associating it. Let me list some of the methods you can refer to:

# #一: The advantage of querying through MongoDB's mapreduce is that there is no data redundancy or data inconsistency, and there is no need for multiple queries. The disadvantage is that MongoDB's MapReduce is based on the JavaScript engine (currently Spider monkey), single-threaded, so inefficient, not suitable for real-time query # #二: Through a good design data model to do

VarPostschema= New Schema({Author: {Type: String},Title: {Type: String, Require : True},Content: {Type: String, Require : True},Comment: {Owner: {Type: String},Subject: {Type: String, Require},Content: {TypeString, require} } }); mypostmodel. ({  ' comment.subject '  : /car*/ }).  (function (err,< Span class= "PLN" > Result) { do some stuff Span class= "KWD" >with the Result ... });             
ym1623 3/F • 3 years ago 1

By the way, add, and use ref for correlation, for example:

VarMongoose= Require(' Mongoose ') , Schema =Mongoose.Schema Var Personschema = New Schema({Name: String,Age: Number,Stories: [{Type: Schema.Types.ObjectId, Ref: ' Story ' }]});Var Storyschema = New Schema({_creator: {Type: Schema.Types.ObjectId, Ref: ' Person ' },Title: String,Fans: [{Type: Schema.Types.ObjectId, Ref: ' Person ' }]});Var Story =Mongoose.Model(' Story ', Storyschema);Var Person =Mongoose.Model(' Person ', Personschema);#关联查询Story.FindOne({Title: /timex/ }).Populate(' _creator ').Exec(function (err, Story) { if  ( Err  return HandleError err Console.log ( ' The creator is%s ' , Story _creator. //prints "the creator is Aaron" })    
a272121742 4/F • 3 years ago

Well, populate is a good way to try to find him more rigorous after many times, need to match completely exhausted Model_name and schema_filed. You can do this in the middle with a ignore case.

a272121742 5/F • 3 years ago

Want to ask, triple query How to do? Like A-ref-b,b-ref-c, find out ABC when we check A.

A:{    B:{ C:{ } }}
ym1623 6/F • 3 years ago

@a272121742 actually you see the Storyschema model is a two-link, but at the same level, to you this interrelated words really not suitable for this method, can only be done by the second method of designing the data model, If you force the use of MapReduce first with populate words too much performance, you so much related data, suggest you mysql+mongodb simultaneous use, relational data with MySQL package npm install mysql require ' MySQL ', if it's going to sacrifice performance as a cost, it doesn't make sense with MongoDB.

Mongoose how to implement statistical query, correlated query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.