简体   繁体   English

NodeJS、MongoDB、$lookup 结果的聚合函数为空数组 []

[英]NodeJS, MongoDB, Aggregation function with $lookup result is empty array []

my grades model is:我的成绩模型是:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var GradeSchema = new Schema({
gID: {type:Schema.Types.ObjectId,ref: 'People'},
    grade: Number,
    type: Number
}, {timestamps: true});

var Grade = mongoose.model('Grade', GradeSchema);
module.exports=Grade;

people model is:人员模型是:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var PeopleSchema = new Schema({
_id: {type:Schema.Types.ObjectId,ref:'Grade' },
    name: String,
    lastName:String,
    phone: String
},{timestamps: true});

var People = mongoose.model('People', PeopleSchema);

module.exports=People;

my aggregate query is:我的汇总查询是:

Grade.aggregate([

      {$lookup:{ from: 'People', localField:'glD', 
        foreignField:'_id',as:'myCustomResut'}},
]).exec((err, result)=>{
      if (err) {
          console.log("error" ,err)
      }
      if (result) {
          console.log(result);
      }
});

** but myCustomResut is empty result like myCustomResut []: ** what is wrong with this code? ** 但 myCustomResut 是空结果,如 myCustomResut []: ** 这段代码有什么问题?

[ { _id: 5a13e33e931f7561b85d0840, updatedAt: 2017-11-21T08:26:38.413Z, createdAt: 2017-11-21T08:26:38.413Z, gID: 5a13e33e931f7561b85d083f, grade: 20, type: 2, __v: 0, myCustomResut:: [] }, { _id: 5a13e78e4fac5b61ecdbd9ab, updatedAt: 2017-11-21T08:45:02.517Z, createdAt: 2017-11-21T08:45:02.517Z, gID: 5a13e78e4fac5b61ecdbd9aa, grade: 20, type: 2, __v: 0, myCustomResut:: [] } ] [ { _id: 5a13e33e931f7561b85d0840, 更新时间: 2017-11-21T08:26:38.413Z, createdAt: 2017-11-21T08:26:38.413Z, gID: 370d2f3e, 自定义等级: 370d08:26:38.413Z, 370d2f3v, 378d2f3v, 3703 ut3v : [] }, { _id: 5a13e78e4fac5b61ecdbd9ab, updatedAt: 2017-11-21T08:45:02.517Z, createdAt: 2017-11-21T08:45:02.517Z, gID:79bd9a, ec3fa: 20100000,15a, 2017-11-21T08:45:02.517a 0, myCustomResut:: [] } ]

Check if your collection is really People .检查您的收藏是否真的是People From experience I know that people created a collection with capital letter but in their database it was with small.根据经验,我知道人们用大写字母创建了一个集合,但在他们的数据库中它是小写的。 So check if it is not people instead.所以检查它是否不是people It has to do with Mongoose.这与猫鼬有关。 So try your aggregation again like that:所以再次尝试你的聚合:

Grade.aggregate([

      {$lookup:{ from: 'people', localField:'gID', 
        foreignField:'_id',as:'myCustomResut'}},
]).exec((err, result)=>{
      if (err) {
          console.log("error" ,err)
      }
      if (result) {
          console.log(result);
      }
});

Here you defined your field gID (upper case i):您在这里定义了您的字段gID (大写 i):

var GradeSchema = new Schema({ gID : {type:Schema.Types.ObjectId,ref: 'People'} var GradeSchema = new Schema({ gID : {type:Schema.Types.ObjectId,ref: 'People'}

And here you wrote glD (lower case L):在这里你写了glD (小写 L):

{$lookup:{ from: 'People', localField:' glD ' {$lookup:{ from: 'People', localField:' glD '

I updated the code snippet above, so try again, with the collection as People or people .我更新了上面的代码片段,所以再试一次,集合为Peoplepeople But the mistake is definitely the glD.但错误肯定是glD。 It was also tricky for me to see, because if you read the code like that, it doesn't look like there is a problem.这对我来说也很棘手,因为如果你像那样阅读代码,看起来并没有问题。 I only realized it's wrong when I went to edit my answer.当我去编辑我的答案时,我才意识到这是错误的。

try to write collection name peoples instead of people尝试写集合名称peoples而不是people

 Grade.aggregate([

      {$lookup:{ from: 'peoples', localField:'gID', 
        foreignField:'_id',as:'myCustomResut'}},
]).exec((err, result)=>{
      if (err) {
          console.log("error" ,err)
      }
      if (result) {
          console.log(result);
      }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM