Startec
Startec

Reputation: 13206

Mongoose: Why can I find a document when a parameter is a string but not a number in a query?

I have an object in my mongo database that looks like this:

{
   name: 'test',
   sku: 11223344
}

I am trying to find the item by sku like such:

Products.findOne({sku: 11223344}, function (err, product) {
  if (product) {
   // Do something
  }
  return
});

However this never returns the item. I can inspect my database with Robomongo and I can see that I am typing the number exactly right and that type of sku in mongo is double.

Strangely, when I convert the sku to a string, and pass in a string, I can find the item.

Is there some sort of limit to precision, or some reason that I would be able to find an item when the query property is specified as a string but not a number?

Here is my Schema for the item:

var mongoose = require('mongoose');                                                                                                                                                                                 

var Schema = mongoose.Schema;                                                                                                                                                                                       

var productSchema = new Schema({                                                                                                                                                                                    
  // T-Shirt, jeans, etc                                                                                                                                                                                            
  kind: {                                                                                                                                                                                                           
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  title: {                                                                                                                                                                                                          
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  edition: {                                                                                                                                                                                                        
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  urlEdition: {                                                                                                                                                                                                     
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  flavor: {                                                                                                                                                                                                         
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  urlFlavor: {                                                                                                                                                                                                      
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  itemDetails: {                                                                                                                                                                                                    
    type: Array                                                                                                                                                                                                     
  },                                                                                                                                                                                                                
  sizes: {                                                                                                                                                                                                          
    type: {}                                                                                                                                                                                                        
  },                                                                                                                                                                                                                
  sizeGuide: {                                                                                                                                                                                                      
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  description: {                                                                                                                                                                                                    
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  shortDescription: {                                                                                                                                                                                               
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  aboutSpecific: {                                                                                                                                                                                                  
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  careInstructions: {                                                                                                                                                                                               
    type: String                                                                                                                                                                                                    
  },                                                                                                                                                                                                                
  images: {                                                                                                                                                                                                         
    type: {}                                                                                                                                                                                                        
  },                                                                                                                                                                                                                
  sku: {                                                                                                                                                                                                            
    type: String                                                                                                                                                                                                    
  }                                                                                                                                                                                                                 
});                                                                                                                                                                                                                 

Thanks for any help.

Upvotes: 0

Views: 475

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312035

The field definitions of your schema need to match the documents in the collection for queries to work properly as Mongoose performs value casting based on the schema.

So in this case sku needs to be defined as a number instead:

sku: {
    type: Number
}

Upvotes: 1

Related Questions