Home Web Front-end JS Tutorial nodejs+mongodb aggregate cascade query operation example

nodejs+mongodb aggregate cascade query operation example

May 29, 2018 pm 04:36 PM
mongodb nodejs

This article mainly introduces the nodejs mongodb aggregate cascade query operation, and analyzes the related operation skills of the nodejs-based mongodb database cascade query in the form of examples. Friends in need can refer to the following

The examples in this article describe nodejs mongodb aggregate cascade query operation. Share it with everyone for your reference, the details are as follows:

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);
Copy after login

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);
Copy after login

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);
Copy after login

What we need to do here is to organize and sort the soft articles by accountId→aggregate→cascade 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);
        }));
      })
    });
};
Copy after login

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 } ]
Copy after login

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

E-mail address format verification in JavaScript

How to introduce the icon icon into the Vue project

Vue.js implementation of configurable login form code details

The above is the detailed content of nodejs+mongodb aggregate cascade query operation example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Is nodejs a backend framework?

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

What is the difference between npm and npm.cmd files in the nodejs installation directory?

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

Is there a big difference between nodejs and java?

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Is nodejs a back-end development language?

Which one to choose between nodejs and java? Which one to choose between nodejs and java? Apr 21, 2024 am 04:40 AM

Which one to choose between nodejs and java?

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

What are the global variables in nodejs

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

How to deploy nodejs project to server

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

How to connect nodejs to mysql database

See all articles