1

I am retrieving data from my mongoDB database and I want to be able to sort documents by subtraction on length of inner arrays. My data structure looks like:

[
  {
   _id: "6bcdf5b7-0757-4195-a0b6-d7ec3d59d194",
   status: {
      plus: ['1', '2'],
      minus: ['1']
  },
  {
   _id: "6fb2ea76-8f12-4200-98fa-72201ba5f8c1",
   status: {
      plus: ['1', '2', '3', '4'],
      minus: []
  },
  {
   _id:"b1ecf353-acea-4808-a847-27b765c52230"
   status: {
      plus: ['1', '2', '3'],
      minus: ['1', '2', '3']
  },
...
]

What I am looking for - array of docs which is sorted by subtract of lengths plus and minus arrays.

Result, that I am looking for:

[
   {
   _id: "6fb2ea76-8f12-4200-98fa-72201ba5f8c1",
   status: {
      plus: ['1', '2', '3', '4'],
      minus: [] // subtract - 4
  },
  {
   _id: "6bcdf5b7-0757-4195-a0b6-d7ec3d59d194",
   status: {
      plus: ['1', '2'],
      minus: ['1'] // subtrack - 1
  },

  {
   _id:"b1ecf353-acea-4808-a847-27b765c52230"
   status: {
      plus: ['1', '2', '3'],
      minus: ['1', '2', '3'] // subtrack - 0
  },
...
]

I found Aggregation framework, but I cant find a way to use it in my way.

Of course, documents have much more properties in them, so group is not a best way, by my opinion.

So my final result looks like this:

   col.aggregate([
        {
          $project: {
            status_count: {
              $subtract: [
                { $size: { $ifNull: ['$status.plus', []] } },
                { $size: { $ifNull: ['$status.minus', []] } }
              ]
            }
          }
        },
        {
          $sort: { status_count: -1 }
        }
      ])

Problems is: it looses all of other properties, including status object, I cant user additional find query, I cant skip and limit for the pagination.

  • Not a dup imo. Vote to reopen. Most linked answers are using MongoDB from the stoneage with outdated methods. – Daniel W. Apr 18 '19 at 11:09
  • @DanFromGermany Question with no code, pointing to the usage examples of the two things they need to do. How about letting the person asking the question read something and learn. If the question showed an attempt then maybe an answer correcting it would be fine. – Neil Lunn Apr 18 '19 at 11:13
  • @NeilLunn I can show you tons of the code, that didnt work at all, or didnt work as expected. I have looked for the familiar questions for several days, but it not seems like a popular question. Non of your related question are not even near my theme, so I dont understand your hate. Thanks. – Vadim Torosyan Apr 18 '19 at 11:15
  • 1
    @VadimTorosyan By all means show your code attempts. But I also suggest actually looking at the examples you have been pointed to and read them since `$size` returns the array length ( same old answer applies ) and `$sort` is a stage used to "sort" on a projected value ( same old answer applies ). And "subtracting" is odly done with an operator called `$subtract`. If you still cannot work it out after viewing those examples, then include the code you have tried within the question. But do make sure to attempt to use the operators as mentioned and by their usage examples. – Neil Lunn Apr 18 '19 at 11:19
  • @NeilLunn Code added. I am similar to that parameters, I am able to find and sort by subtract, but I am loosing all of my other properties and able to paginate result. – Vadim Torosyan Apr 18 '19 at 11:28
  • `$addFields` instead of `$project`. And basically a new simple duplicate added which you could have been pointed to right away if you only included the code and the actual "but I want all fields" problem and statement in the first place. For future reference, that was the question you were actually asking and should have posted, or rather searched for since it has a very easy to find title to that question. – Neil Lunn Apr 18 '19 at 11:31
  • @NeilLunn Ok, thanks for your time and answer, but last thing, how can I add additional find query? I know, that I cant user .find and .aggregate at the same time. – Vadim Torosyan Apr 18 '19 at 11:39
  • That is better done by [Asking a new Question](https://stackoverflow.com/questions/ask). But you might basically consider that `$match` is just another aggregation stage, and can occur as well as would apply to the stage position it is implemented. Also think of typing the phrase of what you think you are asking into a search engine, since most things you can think of have been answered before. – Neil Lunn Apr 18 '19 at 11:47
  • @NeilLunn Great, thank you very much! – Vadim Torosyan Apr 18 '19 at 11:56

0 Answers0