Pfeiff86
Pfeiff86

Reputation: 142

Mongodb lookup with two ObjectId

I have a little issue getting lookup to work. It returns an empty array. I use contactId field in Billing Collection. And I use the contact _id created when entry in Contact Collection in mongodb (Can see it in Robomongo). I have few Billings with ContactId corresponding to the _id of few Contacts. Is my syntaxe correct ? Do I miss something ? Thank you for your help. Below is my lookup syntaxe

        Contact.aggregate([
            {
                $lookup: {
                    from: "Billing",
                    localField: "_id",
                    foreignField: "contactId",
                    as: "BillingMembership"
                }
            }
        ]).exec(function (err, contacts) {
            if (err) {
                return res.status(500).json({
                    title: 'An error occurred',
                    error: err
                });
            }
            res.status(200).json({
                message: 'Success',
                obj: contacts
            });
        });
Below is the result I get back from the database.

(4) [Object, Object, Object, Object]
0:Object
BillingMembership:Array(0)
length:0
__proto__:Array(0)
additionalInterests:"MFM/REI"
billingEmail:"[email protected]"
cellPhone:6787025500
dateBirth:"1555-02-02T00:00:00.000Z"
firstName:"qtazerqr'efsg"
gogsMbrType:"Resident Applicant"
gogsYearJoined:"20111"
homePhone:6787025500
lastName:"gzaetrsg"
memberSuffix:", DO"
middleName:"fzerqgrre"
notes:"htrfjghdnt"
officeEmail:"[email protected]"
officePhone:6787025500
personalEmail:"[email protected]"
practiceId:"592e4c1638a494089c50c8c8"
praticeType:"MFM/High Risk"
spFirstNm:"gsertdhy"
spLastNm:"rthytrfgj"
spSuffix:"syhtdrh"
website:"trshdty"
__v:0
_id:"5932db29eb4dfe0de4a8a36d"
__proto__:Object
1:Object
2:Object
3:Object

Mongoose Schema Contact

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
    firstName: {type: String, required: true},
    middleName: {type: String, required: true},
    lastName: {type: String, required: true},
    dateBirth: {type: Date, required: true},
    memberSuffix: {type: String, required: true},
    officePhone: {type: Number, required: true},
    homePhone: {type: Number, required: true},
    cellPhone: {type: Number, required: true},
    officeEmail: {type: String, required: true},
    billingEmail: {type: String, required: true},
    personalEmail: {type: String, required: true},
    gogsMbrType: {type: String, required: true},
    gogsYearJoined: {type: String, required: true},
    spFirstNm: {type: String, required: true},
    spLastNm: {type: String, required: true},
    spSuffix: {type: String, required: true},
    notes: {type: String, required: true},
    praticeType: {type: String, required: true},
    additionalInterests: {type: String, required: true},
    website: {type: String, required: true},
    practiceId: {type: Schema.Types.ObjectId, ref: 'Practice'}
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('Contact', schema);

Mongoose Schema Billing

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');

var schema = new Schema({
    reason: {type: String, required: true},
    amount: {type: Number, required: true},
    membership: {type: String, required: true},
    membershipYear: {type: Schema.Types.ObjectId, ref: 'Membership'},
    type: {type: String, required: true},
    date: {type: String, required: true},
    contactId: {type: Schema.Types.ObjectId, ref: 'Contact'}, 
    conferenceId: {type: Schema.Types.ObjectId, ref: 'Conference'}
});

schema.plugin(mongooseUniqueValidator);

module.exports = mongoose.model('Billing', schema);

Upvotes: 1

Views: 556

Answers (1)

Pfeiff86
Pfeiff86

Reputation: 142

Solved. thanks to Veeram.

I used Change your from: "Billing" to from: "billings"

Upvotes: 1

Related Questions