Sunday, January 27, 2013

Mongodb SyntaxError: invalid property id on $or

the problem example:
db.tablename.find(
{"_id.period":{"$gte": ISODate("2011-01-01T00:00:00"), "$lte": ISODate("2011-12-31T23:59:59")}, 
{"$or": [{"value.field1": "COMMUNICATION", "value.field2": "GOVERNMENT INSTITUTIONS - LOCAL"}, {"value.field1": "COMMUNICATION", "value.field2": "INTERNET SERVICE PROVIDER"}]}
})

the solution:
#remove { } wrapper from $or

{"_id.period":{"$gte": ISODate("2011-01-01T00:00:00"), "$lte": ISODate("2011-12-31T23:59:59")}, 
"$or": [{"value.field1": "COMMUNICATION", "value.field2": "GOVERNMENT INSTITUTIONS - LOCAL"}, {"value.field1": "COMMUNICATION", "value.field2": "INTERNET SERVICE PROVIDER"}]
})


1 comment:

Unknown said...

Thanks man, this saved my life!