Devin
Devin

Reputation: 149

Mongoid throw 16052 exception

I want to use mongoid to implement the query funcation likes 'GROUP BY',but i caught an exception:

failed with error 16052: "exception: could not create cursor over th_test.messages for query : { sr_flag: /.*541260c5aee1a93f70000001.*/ } sort : { created_at: -1 }"

My code is here:

def messages
    map = %Q{
      function() {
        emit(this.sr_flag, { count: 1 });
      }
    }

    reduce = %Q{
      function(key, values) {
        var result = { count: 0 };
        values.forEach(function(value) {
          result.count += value.count;
        });
        return result;
      }
    }

    result = Message.where(sr_flag: /.*#{self.id}.*/).map_reduce(map, reduce).out(inline: true).to_a
    result
end

Can someone help me explain why? I had searched a blog.Does the mongoid set the created_at column as primary key?

Upvotes: 0

Views: 185

Answers (1)

Devin
Devin

Reputation: 149

I had fixed my problem.The reason was someone writed a default_scope for my Message model,but the column sorted on was not the key column of the map method.Just Using unscoped method to make program work.

result = Message.unscoped.where(sr_flag: /.*#{self.id}.*/).map_reduce(map, reduce).out(inline: true).to_a

Upvotes: 1

Related Questions