MongoDB / Mongoose - 查询$ gte和$ lte无法正常工作

时间:2015-07-26 23:57:07

标签: mongodb mongoose

所以我现在正在使用MongoDB的mongoose库进行查询,以便在一系列高度之间返回所有用户。它返回null,即使我设置了req.body.heightMin和req.body.heightMax参数,我知道这些参数将与我设置的测试用户匹配。我相信我的语法是正确的,因为如果我从请求体中删除了heightMin和heightMax,则所有其他搜索参数都有效。任何见解都会非常感激。谢谢!

//Height
if(req.body.heightMin && req.body.heightMax){
  searchTerm.profileHeight = { '$gte': req.body.heightMin, '$lte': req.body.heightMax };
}   

var callback = function(err, users){
  if(err) {
    console.log(err);
    return res.status(400).send({
      message: errorHandler.getErrorMessage(err)
    });
  } else {
    console.log(users);
    res.jsonp(users);
  }
};    

User.find(searchTerm).exec(callback);

模式

/**
 * User Schema
 */
var UserSchema = new Schema({
    firstName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    },
    lastName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your last name']
    },
    uid: {
        type: String
    },
    displayName: {
        type: String,
        trim: true
    },
    email: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your email'],
        match: [/.+\@.+\..+/, 'Please fill a valid email address']
    },
    username: {
        type: String,
        unique: 'Username already exists',
        required: 'Please fill in a username',
        trim: true
    },  
    password: {
        type: String,
        default: '',
        validate: [validateLocalStrategyPassword, 'Password should be longer']
    },
    salt: {
        type: String
    },
    provider: {
        type: String,
        required: 'Provider is required'
    },
    providerData: {},
    additionalProvidersData: {},
    roles: {
        type: [{
            type: String,
            enum: ['user', 'admin']
        }],
        default: ['user']
    },
    updated: {
        type: Date
    },
    created: {
        type: Date,
        default: Date.now
    },
    /* For reset password */
    resetPasswordToken: {
        type: String
    },
    resetPasswordExpires: {
        type: Date
    },
    location: { 
        type: [Number], 
        index: { 
            type: '2dsphere', 
            sparse: true
        },
        default: [-122.4194200, 37.7749300]
    },
    dob: {
        type: Date
    },
    age: {
        type: Number
    },
    gender: {
        type: String,
        default: 'Man'
    },
    city: {
        type: String,
        default: 'New York City'
    },
    state: {
        type: String,
        default: 'NY'
    },
    zipcode: {
        type: String,
        default: '10001'
    },
    photos: {
        type: [{
          photo: {
            type: String
          },
          crop: {
            type: String
          }
        }]
    },
    videos: {
        type: [{
          video: {
            type: String
          },
          preview: {
            type: String
          }
        }]
    },
    school: {
        type: String
    },
    profileHeight: {
        type: Number
    },
    profileWeight: {
        type: Number
    },
    profilePrimaryPosition: {
        type: String
    },
    profileSecondaryPosition: {
        type: String
    },
    stat40: {
        type: Number
    },
    statBenchReps: {
        type: Number
    },
    statVertical: {
        type: Number
    },
    statBroadJump: {
        type: Number
    }

});

Mongo中的对象

{
    "_id": {
        "$oid": "55b5a39fe4b01d0a17cdec23"
    },
    "firstName": "John",
    "lastName": "Wu",
    "uid": "120058400494882",
    "displayName": "John Wu",
    "email": "johnwu@testingemail.wu",
    "username": "JohnWu4U",
    "dob": "01/01/1990",
    "age": 25,
    "gender": "Man",
    "city": "New York",
    "state": "New York",
    "zipcode": "10001",
    "photos": [],
    "videos": [],
    "school": "Test High School",
    "profileHeight": 72,
    "profileWeight": 185,
    "profilePrimaryPosition": "1B",
    "profileSecondaryPosition": "3B",
    "stat40": 4.15,
    "statBenchReps": 24,
    "statVertical": 25,
    "statBroadJump": 12.62
}

请求

在邮递员中,我设置了heightMin = 0和heightMax = 1000,以确保我能得到结果。

0 个答案:

没有答案