fetch data between min and max price from mongodb using node js

204
January 16, 2018, at 5:51 PM

I am trying to fetch data between min and max price from property model

e.g.if user inputs min and max price then the number of properties between that min and max price will show

i have indexed the $text in for type and tags value

I am new to mongodb.. appreciate the help. thankyou

here is my controller code

searchProperty = async (req, res) => {
const property = await Property
// first find the property that matches
.find({
    $text: {
        $search: req.query.q
    }
}, {
    score: { $meta: 'textScore' }
})
// then sort them
.sort({
    score: { $meta: 'textScore' }
})
// limit to only 5 result
//.limit(5)
res.json(property); };

here is my model

const propertySchema = new mongoose.Schema({
name: {
    type: String,
    trim: true,
    required: 'Please enter your name!'
},
status: {
    type: String,
    required: 'Please enter the status!'
},
type: {
    type: String,
    required: 'Please enter your type!'
},
rooms: {
    type: String,
    required: 'Please enter your no of Rooms!'
},
old: String,
bedrooms: String,
bathrooms: String,
phone: Number,
title: {
    type: String,
    trim: true,
    required: 'Please enter your property title!'
},
price: {
    type: Number,
    required: 'Please enter your Price!'
},
area: Number,
description: {
    type: String,
    trim: true,
    required: 'Please enter your description!'
},
zipcode: Number,
slug: String,
tags: [String],
created: {
    type: Date,
    default: Date.now
},
location: {
    type: {
        type: String,
        default: 'Point'
    },
    coordinates: [{
        type: Number,
        required: 'Please enter the Coordinates!'
    }],
    address: {
        type: String,
        required: 'Please enter the Address!'
    }
},
author: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    reuqired: 'You must supply an author'
} });
   // Define our indexes
   propertySchema.index({
    type: 'text',
    tags: 'text'
  });

here is my route

router.get('/api/v1/search', searchProperty);
Answer 1

You can use $gte and $lte for this.

db.properties.find({price: { $gte: 1, $lte: 1000 } } );

This will fetch the properties with the price between 1 and 1000.

Rent Charter Buses Company
READ ALSO
how to create duplicate list in trello clone using mongoose node.js [on hold]

how to create duplicate list in trello clone using mongoose node.js [on hold]

The hierarchy of List: List -> Items -> (attachements,comments,labels)

239
How to prepare APN for production

How to prepare APN for production

Im trying to deploy my app with notifications but it's giving me the biggest headache in the worldAll other questions ive seen with regards to this seem outdated

250
Accessing gmail from nodejs returns only message id

Accessing gmail from nodejs returns only message id

I keep trying to get the information for my messages, but I only get the id; the rest is undefined:

237
events.js:142 error in node.js and express framework and MongoDB [on hold]

events.js:142 error in node.js and express framework and MongoDB [on hold]

user@lenovo MINGW64 /c/Projects/BookStore $ node app Running on port 3000events

279