this article gives an example of the nodejs+mongodb aggregate cascading query operation. Share for you for your reference, specifically as follows:

recently completed a nodejs+mongoose project, encountered the mongodb cascading query operation. The situation is to implement a rankings to see the top ten of the most effective Ruan chapter Wen of a company (Organization).

Account table: the company's information exists in a single account form.

 var AccountSchema = new Schema ({type: String} password: {type: {loginname:, String} / * * * * / / / contact account company name comName: {type: String}, address: {type: String} / / / / address, the company introduced the intro: {type: String}}); mongoose.model ('Account', AccountSchema); 

Cusomer: the company's customer base.

 var new Schema (CustomerSchema = {/ * * * * / / / {type: basic information code of password: String}, Account belongToAccount: {type:, which belongs to the / / ObjectId, ref: /'Account'}, mobile phone number, login with the mobile: {type: String}, realname: {type: String} / / real name}); CustomerSchema.index ({belongToAccount: 1, mobile: 1}, {unique: true}); mongoose.model ('Customer', CustomerSchema); 


 var articleSchema= new Schema 

({type: ObjectId ref:'Account'} {belongToAccount:, title:, {type: String}, text: {type: String}, createTime: {type: Date, default:}, author: {type: ObjectId, ref:'Customer'}, //0, to be confirmed, 1, -1 status: {type: Number default: 0} is invalid,}); ArticleSchema.index ({belongToAccount: 1, createTime:-1, author: 1}, {unique: false}); mongoose.model ('article', articleSchema);

here to do is, by accountId, aggregate, author and soft finishing sort cascade found on the author's name and other information.

 code as follows: exports.getRankList = function (accountid, callback) {AticleModel.aggregate ({$match: {belongToAccount: mongoose.Types.ObjectId (accountid), status: 1}}, {$group: {_id: {customerId: $author number: {$sum: 1}}}}, {$sort: {number:, -1}}).Limit (10).Exec (function (err, aggregateResult) {{callback (if (ERR) err; return var EP;}) = new (EventProxy); ep.after ('got_customer', aggregateResult.length, function (customerList) {callback (null, customerList);}); aggregateResult.forEach (function (item) {Customer.findOne ({_id: item._id.customerId}, ep.done (function (customer) {item.customerName = customer.realname; item.; / / do someting ep.emit ('got_customer', item);};}}))));}; 

returns a result format (here only two records, as before ten):

 _id: {customerId:} {[559a5b6f51a446602032fs21, number: 5, customerName:'test2', mobile:22 {_id:}, {customerId: 559a5b6f51a446602041ee6f}, number: 1, customerName:'test1', mobile: 

11}] the hope to help the nodejs program design.

you might be interested in this article:

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Nodejs+mongodb aggregate cascaded query operation example | Script Home

You may also be interested in these articles!