用于数据获取的 Mongo 查询

时间:2021-07-24 10:55:16

标签: node.js mongodb aggregate pipeline geonear

我有 3 个不同的系列

  1. 包含租金详细信息、addressId 和 categoryId 的租金收集
const rentItemSchema = mongoose.Schema({
            title: {
                type: String,
                required: true
            },
            description: {
                type: String,
                required: true
            },
            category: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "categories",
                required: true
            },
            address: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "addresses",
                required: true
            },
        }
  1. 地址收集
const addressSchema = new mongoose.Schema({

        streetNumber: {
            type: String,
            max: 200
        },
        locality: {
            type: String,
            max: 200
        },
        landmark: {
            type: String,
            max: 200
        },
        country: {
            type: String,
            max: 5
        },
        postalCode: {
            type: String,
            max: 6
        },
        location: {
            type: {
                type: String,
                max: 5
            },
            coordinates: [{
                type: Number,
                required: true,
                default: 0,
                max: 100
            }]
        },
    )
}
  1. 类别集合
   const categorySchema = new mongoose.Schema({

        name: {
            type: String,
            unique: true,
            //   required: 'Your email/Mobile is required',
        },
        description: {
            type: String,
            max: 200
        },
    )
   };

我想写一个 mongo 查询,它接受 , Keyword , geolocation , distance in KM , Category 并返回结果集

rentitem.Title ="Keyword" ,
rentitem.address.location = "geolocation" and under given KM distance ,
rentitem.category.name = "Category"  

   

我对 MongoDB 查询编写完全陌生,请帮助我并指导我如何编写查询。

0 个答案:

没有答案