4

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

1
  • did u find answer Commented Nov 28, 2017 at 6:41

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

test it by other names but result is myCustomResut[]
thank you my problem solve whene use {$lookup:{ from: 'people', localField:'glD'
@FazelNajariyan if your question was answered. Could you please also mark it as answered?
I have the same issue but I'm not using Mongoose. If I run a query in the Mongodb console it works fine but the same query in NodeJS using the latest version of the 'mongodb' package produces an empty result. Any ideas?
@CleytonT. I'm also having the same issue you had.
0

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);
      }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.