Mongodb在nodejs中填充查询以获得具有填充字段的条件

时间:2016-05-19 14:07:18

标签: node.js mongodb

bellow是我的用户,任务和订单的架构。我想要查询,它可以给我所有分配给特定用户的订单。

  var User =  mongoose.Schema({

        firstName : {
            type: String,
            required: false
        },
        lastName : {
            type: String,
            required: false
        },
        password : {
            type: String,
            required: false
        },
        createdAt:{
            type: Date,
            require: false
        },
        role:{
            type: String,
            require: false
        }
    });


var Task =  mongoose.Schema({

        assignedOn : {
            type:Date
        },
        assignedTo : {type:mongoose.Schema.Types.ObjectId, ref:'User'},
        status : {
            type:String
        },
        priority : {
            type: String
        },
        completeBy:{
            type: Date
        }
    });

var Order =  mongoose.Schema({

        orderDate:{
            type: Date,
            require: false
        },
        completionDate:{
            type: Date,
            require: false
        },
        status: {
            type: String,
            required: false
        },
        orderType: {
            type: String,
            required: false
        },
        remarks: {
            type: String,
            require: false
        },
        totalAmount: {
            type: Number,
            require: false
        },
        items: {
            type: [],
            require: true
        },
        billingAddress: {
            type: IAddress,
            require: true
        },
        shippingAddress: {
            type: IAddress,
            require: true
        },
        defaultTask:  {type:mongoose.Schema.Types.ObjectId, ref:'Task'},
        customer: {type:mongoose.Schema.Types.ObjectId, ref:'Customer'}
    });

我正在做像bellow这样的查询,它没有给我我想要的特定用户_id的正确结果,它给了我所有的订单。

this._orderRepository.findAndPopulate({$match:{assignedTo:_id}},
        {path:'defaultTask ', populate:{path:'items'}, populate:{path:'assignedTo' }}, function(err, result){
            console.log("error =>"+err);
            /*console.log("result=>"+result);*/
            callback(err, result);
        } )

0 个答案:

没有答案