MongoDB question

Posted on 2011-10-28
Last Modified: 2016-02-10
When I want to sort by multiple keys on mongodb , I got some problem , And can I fix it ??





error: { "$err" : "too much data for sort() with no index. add an index or speci fy a smaller limit", "code" : 10128 }
Question by:wsyy
    LVL 46

    Expert Comment


    Author Comment

    Hi Gary, sorry for late response. Quite busy recently.

    Could you please check the link? Can't access to it.
    LVL 46

    Accepted Solution

    Tuesday, February 15, 2011
    Always index columns that you want to sort on in Mongo
    I'm using Mongo as a non-relational database for a few projects. In general it's working out great. MySQL would work too but I like not having to explicitly create a database or run migrations. Plus I figure you can't really understand the strengths and weaknesses of a technology unless you build a real application with it.

    I work in Ruby and use the MongoMapper and Mongoid Object Data Mappers to talk to Mongo.

    One issue that I do not like is the requirement that you explicitly create an index for every column that you think you will want to sort on. If you don't then all the data gets loaded into memory for the sort and you get an error like this:

    ?12 [...]/gems/mongo-1.2.1/lib/mongo/cursor.rb:86:in `next_document': too much data for sort() with no index (Mongo::OperationFailure)

    And if you want to sort on two columns then you need an index on the combination of the two.

    You can add indexes at any point - it takes some action but it's not that big a deal. But it doesn't 'just work'... in MySQL it does - an index might give you better performance but it doesn't blow up without one.

    You'll hear people claim that the NoSQL databases are schema-free, giving you a lot of flexibility. I don't really buy that argument - in most applications you want a clear schema.

    Where I do see the benefit is that, with NoSQL databases, your schema resides your Model - not in the DB itself - and that is where it belongs. When you want to change the schema you just change the Model - no database migrations - very flexible.

    But, with Mongo at least, if you have to define indexes ahead of time in order to sort even relatively small numbers of objects then that nullifies some of that benefit.

    Posted by Robert Jones at 2:30 PM   Labels: ruby mongo mongoid mongomapper mysql

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    top 100 or rank 100 teradata 4 33
    PDO and Database 1 64
    date of death fact tbl or dimension? 15 57
    SQL Update Query 23 65
    Introduction: I have seen many questions on EE and elsewhere, asking about how to find either gaps in lists of numbers (id field, usually) ranges of values or dates overlapping date ranges combined date ranges I thought it would be a good …
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now