使用节点js

时间:2018-01-16 06:14:03

标签: node.js mongodb express mongoose mongodb-query

我正在尝试从属性模型

中获取最低和最高价格之间的数据

例如,如果用户输入最低和最高价格,那么该最低和最高价格之间的属性数量将显示

我已为类型和标签值

索引了$ text

我是mongodb的新手.. 感谢帮助。 三江源

这是我的控制器代码

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

这是我的模特

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

这是我的路线

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

1 个答案:

答案 0 :(得分:1)

您可以使用$gte$lte

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

这将获取价格介于1和1000之间的属性。