Home  >  Article  >  Web Front-end  >  Detailed explanation of nodejs and mongodb aggregate cascade query operations

Detailed explanation of nodejs and mongodb aggregate cascade query operations

小云云
小云云Original
2018-03-19 09:08:142449browse

I recently completed a nodejs+mongoose project and encountered the cascade query operation of mongodb. The situation is to implement a ranking list to view the top ten people who have published the most valid articles among the customers of a certain company (organization).

Account table: Company information exists in a separate account table.


var AccountSchema = new Schema({
  loginname: {type: String},
  password: {type: String},
  /**
   * 联系方式
   */
  //账户公司名
  comName: {type: String},
  //地址
  address: {type: String},
  //公司介绍
  intro: {type: String}
});
mongoose.model('Account', AccountSchema);

Cusomer table: The company's customer base.


var CustomerSchema = new Schema({
  /**
   * 基本信息
   */
  //密码
  password: {type: String},
  //归属于哪个Account
  belongToAccount: {type: ObjectId, ref: 'Account'},
  //手机号,登录用
  mobile: {type: String},
  //真实姓名
  realname: {type: String}
});
CustomerSchema.index({belongToAccount: 1, mobile: 1}, {unique: true});
mongoose.model('Customer', CustomerSchema);

article table


var articleSchema= new Schema({
  belongToAccount: {type: ObjectId, ref: 'Account'},
  title: {type: String},
  text: {type: String},
  createTime: {type: Date, default: Date.now},
  author: {type: ObjectId, ref: 'Customer'},
  //0,待确认,1 有效 ,-1 无效
  status: {type: Number, default: 0}
});
articleSchema.index({belongToAccount: 1, createTime:-1,author: 1}, {unique: false});
mongoose.model('article', articleSchema);

What we need to do here is to organize and sort the soft articles by accountId→aggregate→level Link author to find the author's name and other information.

The code is 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) {
    if(err){
      callback(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.customerMobile=cusomer.mobile;
          // do someting
          ep.emit('got_customer', item);
        }));
      })
    });
};

The returned result format (there are only two records here, actually the top ten):


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

Related recommendations:

sql Multi-level classification cascade query

The above is the detailed content of Detailed explanation of nodejs and mongodb aggregate cascade query operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn