nodejs+mongodb aggregate級(jí)聯(lián)查詢操作示例
本文實(shí)例講述了nodejs+mongodb aggregate級(jí)聯(lián)查詢操作。分享給大家供大家參考,具體如下:
最近完成了一個(gè)nodejs+mongoose的項(xiàng)目,碰到了mongodb的級(jí)聯(lián)查詢操作。情形是實(shí)現(xiàn)一個(gè)排行榜,查看某個(gè)公司(organization)下屬客戶中發(fā)表有效文ruan章wen最多的前十人。
Account表:公司的信息單獨(dú)存在一個(gè)account表里。
var AccountSchema = new Schema({
loginname: {type: String},
password: {type: String},
/**
* 聯(lián)系方式
*/
//賬戶公司名
comName: {type: String},
//地址
address: {type: String},
//公司介紹
intro: {type: String}
});
mongoose.model('Account', AccountSchema);
Cusomer表:公司的客戶群。
var CustomerSchema = new Schema({
/**
* 基本信息
*/
//密碼
password: {type: String},
//歸屬于哪個(gè)Account
belongToAccount: {type: ObjectId, ref: 'Account'},
//手機(jī)號(hào),登錄用
mobile: {type: String},
//真實(shí)姓名
realname: {type: String}
});
CustomerSchema.index({belongToAccount: 1, mobile: 1}, {unique: true});
mongoose.model('Customer', CustomerSchema);
article表
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,待確認(rèn),1 有效 ,-1 無(wú)效
status: {type: Number, default: 0}
});
articleSchema.index({belongToAccount: 1, createTime:-1,author: 1}, {unique: false});
mongoose.model('article', articleSchema);
這里要做的就是,由accountId→aggregate整理軟文并排序→級(jí)聯(lián)author找到作者的姓名及其他信息。
代碼如下:
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);
}));
})
});
};
返回的結(jié)果格式(這里僅有兩條記錄,實(shí)際為前十):
[ { _id: { customerId: 559a5b6f51a446602032fs21 }, number: 5,
customerName: 'test2',
mobile:22 } ,
{ _id: { customerId: 559a5b6f51a446602041ee6f }, number: 1,
customerName: 'test1',
mobile: 11 } ]
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
相關(guān)文章
Nodejs中koa2連接mysql的實(shí)現(xiàn)示例
本文主要介紹了Nodejs中koa2連接mysql的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
使用 Node.js 模擬滑動(dòng)拼圖驗(yàn)證碼操作的示例代碼
本篇文章主要介紹了使用 Node.js 模擬滑動(dòng)驗(yàn)證碼操作的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
nodejs簡(jiǎn)單訪問(wèn)及操作mysql數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了nodejs簡(jiǎn)單訪問(wèn)及操作mysql數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了nodejs創(chuàng)建mysql連接、執(zhí)行sql語(yǔ)句及關(guān)閉連接等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
node-sass@4.14.1報(bào)錯(cuò)的最終解決方案分享
最近在安裝node-sass@4.14.1的時(shí)候遇到了些問(wèn)題,所以下面這篇文章主要給大家介紹了關(guān)于node-sass@4.14.1報(bào)錯(cuò)的最終解決方案,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

