Thursday 13 April 2017

how not to show items with specific condition in mongoose

My code is as shown below:

foodtruck.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Items = require('./items.js');


var FoodTruckSchema = new Schema({
    foodtruck_name:String,
    foodtruck_location:String,
    foodtruck_rating:{type:Number,default:5},
    foodtruck_total_votes:{type:Number,default:0},
    foodtruck_tag:String,
    foodtruck_open_status:{type:Number,default:1},   //0 open 1 closed
    foodtruck_starting_timing:String,
    foodtruck_closing_timing:String,
    foodtruck_cusine:[String],
    foodtruck_img:String,
    foodtruck_logo:String,
    item_list: [ {type : mongoose.Schema.ObjectId, ref : 'items'}]
},{ versionKey: false });



module.exports = mongoose.model('foodtruck',FoodTruckSchema);

items.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ItemSchema = new Schema({
    no_of_times_ordered:Number,
    item_name:String,
    item_tag:String,
    item_category:String,
    item_description:String,
    item_illustrations:[String],
    item_stock:Number,   //0 available 1 last 5 items 2 not available
    item_quantity_ordered:{type:Number,default:0},
    item_discount_price:Number,
    item_price:Number,
    item_img:String,
    no_of_likes:{type:Number,default:0}
},{ versionKey: false });

module.exports = mongoose.model('items',ItemSchema);

My query is as show below:

var foodtrucklist = function(req, res) {
    foodtr.find().populate('item_list').exec(function(err, foodtrucks) {
        foodtrucks.forEach(function(ftr) {
            var start_time = ftr.foodtruck_starting_timing;
            var end_time = ftr.foodtruck_closing_timing;
            var foodtruck_open_status = ftr.foodtruck_open_status;
            // var shut_down = ftr.foodtruck_shutdown;
            if ((start_time && end_time) &&
                (start_time.trim() != '' &&
                    end_time.trim() != '')) {
                if (inTime(start_time, end_time) &&
                    foodtruck_open_status ==0 ) {
                    ftr.foodtruck_open_status = 0;
                    ftr.save();
                } else {
                    ftr.foodtruck_open_status = 1;
                    ftr.save();
                }
            }
        })

        res.json({
            status: '200',
            message: 'foodtrucklist',
            data: foodtrucks
        });
    });
};

now what I want to achieve is, I don't want to show items which have **item_stock = -1 **. How can I achieve in populate query?



via Mrugesh Thaker

No comments:

Post a Comment