Node.js 使用节点js从mongodb获取最低和最高价格之间的数据

Node.js 使用节点js从mongodb获取最低和最高价格之间的数据,node.js,mongodb,express,mongoose,mongodb-query,Node.js,Mongodb,Express,Mongoose,Mongodb Query,我试图从属性模型中获取最小和最大价格之间的数据 e、 g.如果用户输入最小和最大价格,则显示最小和最大价格之间的房地产数量 我已经为$text中的type和tags值编制了索引 我是mongodb的新手。。 谢谢你的帮助。 谢谢 这是我的控制器代码 searchProperty = async (req, res) => { const property = await Property // first find the property that matches .find({

我试图从属性模型中获取最小和最大价格之间的数据

e、 g.如果用户输入最小和最大价格,则显示最小和最大价格之间的房地产数量

我已经为$text中的type和tags值编制了索引

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

您可以为此使用
$gte
$lte

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

这将获取价格介于1和1000之间的房产。

您可以使用
$gte
$lte
进行此操作

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

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

如果要获取最小值和最大值的结果,为什么要限制结果?我建议您使用包含$text的$match聚合作为第一阶段,然后在价格上再次使用包含$gte和$lte的$match。请参阅下文-我已经注释掉了代码中的限制,并感谢提示@Avijwhy您为什么要限制您的结果,如果您想获得min和max的结果?我建议您使用包含$text的$match聚合作为第一阶段,然后在价格上再次使用包含$gte和$lte的$match。请参阅下文-我已经注释掉了代码中的限制,并感谢@Avijgod的提示,这很简单。。1行代码。。这就是你所需要的。谢谢你的邀请help@RahulGupta如果答案对你有帮助,你能接受吗?天哪,答案很简单。。1行代码。。这就是你所需要的。谢谢你的邀请help@RahulGupta如果答案对你有帮助,你能接受吗?