mongodb: best way to get specific documents and then the rest

lets say I have 1000 documents where each one has:

user_id text

Now, I would like to pull all those documents but first pull the documents from a few specific users (given an array of user ids) and then all the rest.

I was thinking to use map reduce to create a new weight inline attribute if the user_id exists in the specific users array (using scope to pass the array) and then to sort that new attribute. But from what I could understand, you can not sort after map reduce.

Any one has a good suggestion how to pull this off? Any suggestion will be welcome.



Well there isn't a lot of detail here, but I can give a sample case for consideration. Consider the following set of documents:

{ "user" : "fred", "color" : "black" } { "user" : "bill", "color" : "blue" } { "user" : "ted", "color" : "red" } { "user" : "ted", "color" : "black" } { "user" : "fred", "color" : "blue" } { "user" : "bill", "color" : "red" } { "user" : "bill", "color" : "orange" } { "user" : "fred", "color" : "orange" } { "user" : "ted", "color" : "orange" } { "user" : "ally", "color" : "orange" } { "user" : "alice", "color" : "orange" } { "user" : "alice", "color" : "red" } { "user" : "bill", "color" : "purple" }

So suppose you want to bubble the items for the users "bill" and "ted" to the top of your results, then everything else sorted by the user and the color. What you can do is run the documents through a $project stage in aggregate, as follows:

db.bubble.aggregate([ // Project selects the fields to show, and we add a weight value {$project: { _id: 0, "user": 1, "color": 1, "weight": {$cond:[ {$or: [ {$eq: ["$user","bill"]}, {$eq: ["$user","ted"]} ]}, 1, 0 ]} }}, // Then sort the results with the `weight` first, then `user` and `color` {$sort: { weight: -1, user: 1, color: 1 }} ])

So what that does is conditionally assign a value to weight based on whether the user was matched to one of the required values. Documents that do not match are simply given a 0 value.

When we move this modified document on to the $sort phase, the new weight key can be used to order the results so the "weighted" documents are on top, and anything else will then follow.

There a quite a few things you can do to $project a weight in this way. See the operator reference for more information:



  • Equivalent to ajaxSend and/or ajaxComplete with jsonp?
  • Excel 2010 search for text in IF function - separate cell data
  • Date format jqplot categoryaxisrenderer after ticks add
  • How to handle TRegEx named capture groups that might be empty?
  • Fake http server for testing purpose
  • What's the difference between [String] vs. [(String)]?
  • how to handle large size of update query in mysql with laravel
  • VB.NET - RichTextBox - Apply formatting to selected text
  • How to show a specific VC , more like navigate to a stack of VC
  • Can you render a page with multiple forms instead of one form for a jQuery, Rails 3 update?
  • Firebase firestore cloud functions showing trigger type 'unknown'
  • Is it mandatory to have a doGet or doPost method?
  • Adding independent aspx/asmx pages into DotNetNuke
  • Where these are stored?
  • Application level floating views with navigation in Android
  • how do i write assembly code from c#?
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • How to use carriage return with multiple line?
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Using jQuery closest() method with class selector
  • Linq Objects Group By & Sum
  • Different response to non-authenticated users and AJAX calls
  • Array.prototype.includes - not transformed with babel
  • Optimizing database types to compact database (SQLite)
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to make Safari send if-modified-since header?
  • Do create extension work in single-user mode in postgres?
  • How to pass list parameters for each object using Spring MVC?
  • Buffer size for converting unsigned long to string
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • reshape alternating columns in less time and using less memory