Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个集合和mongoose的聚合函数相结合,NodeJS_Javascript_Mongodb_Mongoose_Aggregate Functions_Keystonejs - Fatal编程技术网

Javascript 将两个集合和mongoose的聚合函数相结合,NodeJS

Javascript 将两个集合和mongoose的聚合函数相结合,NodeJS,javascript,mongodb,mongoose,aggregate-functions,keystonejs,Javascript,Mongodb,Mongoose,Aggregate Functions,Keystonejs,模型文件:models/artistStatistic.js var artistSchema = new Schema({ name: { type: String, }, },{ timestamps: true }); var artistStatisticSchema = new Schema({ _artistId: { type: Schema.Types.ObjectId, }, monthlyListen

模型文件:
models/artistStatistic.js

var artistSchema = new Schema({
        name: { type: String, },
    },{
        timestamps: true 
});
var artistStatisticSchema = new Schema({
        _artistId: { type: Schema.Types.ObjectId, },
        monthlyListeners: { type: Number, },
    },{
        timestamps: true 
});
var keystone = require('keystone');
const Artist = require('../models/artist');

exports.view = function (req, res) {
    var view = new keystone.View(req, res);
    var locals = res.locals;
    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'homepage';
    getAllArtists(function (artistData) {
        view.render('homepage', { artists: artistData, });
    });
}

getAllArtists = function (artistCallback) {
    console.log("getAllArtists before");
    Artist.aggregate([{
            '$lookup': {
                'from': 'artist_statistics',
                'as': 'statistics',
                'let': { 'id': '$_id' },
                'pipeline': [{
                    '$match': {
                        '$expr': { '$eq': [ '$$id', '$_artistId' ] }
                    }
                }, {
                    '$sort': {
                        'createAt': -1
                    }
                }, {
                    '$limit': 1
                }]
            }
        }, {
            '$unwind': {
                'path': '$statistics'
            }
        }]).cursor({}).exec(function (err, artists) {
        if (err) return console.log(err);

        console.log("getAllArtists exec: ");
        console.log(artists);
        return artistCallback(artists ?? [{name: "Error!!!", statistic: { monthlyListeners: "Error!!!" }}]);
    });
    console.log("getAllArtists after");
}
模型文件:
models/artist.js

var artistSchema = new Schema({
        name: { type: String, },
    },{
        timestamps: true 
});
var artistStatisticSchema = new Schema({
        _artistId: { type: Schema.Types.ObjectId, },
        monthlyListeners: { type: Number, },
    },{
        timestamps: true 
});
var keystone = require('keystone');
const Artist = require('../models/artist');

exports.view = function (req, res) {
    var view = new keystone.View(req, res);
    var locals = res.locals;
    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'homepage';
    getAllArtists(function (artistData) {
        view.render('homepage', { artists: artistData, });
    });
}

getAllArtists = function (artistCallback) {
    console.log("getAllArtists before");
    Artist.aggregate([{
            '$lookup': {
                'from': 'artist_statistics',
                'as': 'statistics',
                'let': { 'id': '$_id' },
                'pipeline': [{
                    '$match': {
                        '$expr': { '$eq': [ '$$id', '$_artistId' ] }
                    }
                }, {
                    '$sort': {
                        'createAt': -1
                    }
                }, {
                    '$limit': 1
                }]
            }
        }, {
            '$unwind': {
                'path': '$statistics'
            }
        }]).cursor({}).exec(function (err, artists) {
        if (err) return console.log(err);

        console.log("getAllArtists exec: ");
        console.log(artists);
        return artistCallback(artists ?? [{name: "Error!!!", statistic: { monthlyListeners: "Error!!!" }}]);
    });
    console.log("getAllArtists after");
}
控制器文件:
controllers/artistController.js

var artistSchema = new Schema({
        name: { type: String, },
    },{
        timestamps: true 
});
var artistStatisticSchema = new Schema({
        _artistId: { type: Schema.Types.ObjectId, },
        monthlyListeners: { type: Number, },
    },{
        timestamps: true 
});
var keystone = require('keystone');
const Artist = require('../models/artist');

exports.view = function (req, res) {
    var view = new keystone.View(req, res);
    var locals = res.locals;
    // locals.section is used to set the currently selected
    // item in the header navigation.
    locals.section = 'homepage';
    getAllArtists(function (artistData) {
        view.render('homepage', { artists: artistData, });
    });
}

getAllArtists = function (artistCallback) {
    console.log("getAllArtists before");
    Artist.aggregate([{
            '$lookup': {
                'from': 'artist_statistics',
                'as': 'statistics',
                'let': { 'id': '$_id' },
                'pipeline': [{
                    '$match': {
                        '$expr': { '$eq': [ '$$id', '$_artistId' ] }
                    }
                }, {
                    '$sort': {
                        'createAt': -1
                    }
                }, {
                    '$limit': 1
                }]
            }
        }, {
            '$unwind': {
                'path': '$statistics'
            }
        }]).cursor({}).exec(function (err, artists) {
        if (err) return console.log(err);

        console.log("getAllArtists exec: ");
        console.log(artists);
        return artistCallback(artists ?? [{name: "Error!!!", statistic: { monthlyListeners: "Error!!!" }}]);
    });
    console.log("getAllArtists after");
}
当没有.cursor({})参数时,它将给出以下错误。然后,当我添加.cursor({})参数时,尽管我发送了请求,但它不会加载页面,也不会运行模型回调函数

Before.cursor({})参数

getAllArtists before
getAllArtists after
Error [MongoError]: The 'cursor' option is required, except for aggregate with the explain argument
    at Function.MongoError.create (/Users/cemil.colak/WebServer/program/statistic/node_modules/mongodb-core/lib/error.js:31:11)
    at /Users/cemil.colak/WebServer/program/statistic/node_modules/mongodb-core/lib/connection/pool.js:485:72
    at authenticateStragglers (/Users/cemil.colak/WebServer/program/statistic/node_modules/mongodb-core/lib/connection/pool.js:431:16)
    at Connection.messageHandler (/Users/cemil.colak/WebServer/program/statistic/node_modules/mongodb-core/lib/connection/pool.js:465:5)
    at Socket.<anonymous> (/Users/cemil.colak/WebServer/program/statistic/node_modules/mongodb-core/lib/connection/connection.js:319:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:302:12)
    at readableAddChunk (_stream_readable.js:278:9)
    at Socket.Readable.push (_stream_readable.js:217:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  ok: 0,
  errmsg: "The 'cursor' option is required, except for aggregate with the explain argument",
  code: 9,
  codeName: 'FailedToParse'
}
GET /example - - ms
getAllArtists before
getAllArtists after
我想做什么;编写查询,以便在从艺术家集合中提取数据时,艺术家统计集合中的数据获得每个艺术家的最后一个值

当我在节点JS中写入时,使用MongoDB Compass的查询不起作用

使用MongoDB Compass将管道导出到语言

[
  {
    '$lookup': {
      'from': 'artist_statistics', 
      'as': 'statistics', 
      'let': {
        'id': '$_id'
      }, 
      'pipeline': [
        {
          '$match': {
            '$expr': {
              '$eq': [
                '$$id', '$_artistId'
              ]
            }
          }
        }, {
          '$sort': {
            'createAt': -1
          }
        }, {
          '$limit': 1
        }
      ]
    }
  }, {
    '$unwind': {
      'path': '$statistics'
    }
  }
]