使用两个ObjectId进行Mongodb查找

时间:2017-07-09 17:12:02

标签: node.js mongodb lookup aggregation objectid

我在查找工作时遇到了一些问题。它返回一个空数组。我在Billing Collection中使用contactId字段。我使用在mongodb中的Contact Collection中输入时创建的联系_id(可以在Robomongo中看到它)。我有几个Billings与ContactId对应少数联系人的_id。我的语法是否正确?我错过了什么吗?谢谢您的帮助。 下面是我的查找语法

        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
            });
        });
以下是我从数据库返回的结果。

(4) [Object, Object, Object, Object]
0:Object
BillingMembership:Array(0)
length:0
__proto__:Array(0)
additionalInterests:"MFM/REI"
billingEmail:"john@netdr.net"
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:"john@netdr.net"
officePhone:6787025500
personalEmail:"john@netdr.net"
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联系

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

1 个答案:

答案 0 :(得分:0)

解决。感谢Veeram。

我用过改变你的:" Billing"来自:"账单"