Fazel Najariyan
Fazel Najariyan

Reputation: 77

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?

[ { _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:: [] } ]

Upvotes: 4

Views: 11968

Answers (2)

Rohit Mankotia
Rohit Mankotia

Reputation: 1

try to write collection name peoples instead of 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);
      }
});

Upvotes: 0

Alex P.
Alex P.

Reputation: 3171

Check if your collection is really 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. 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):

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

And here you wrote glD (lower case L):

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

I updated the code snippet above, so try again, with the collection as People or people. But the mistake is definitely the 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.

Upvotes: 4

Related Questions