4

How to get the maximum of sections.Id in below document where collection._id = some parameter

{
    "_id" : ObjectId("571c5c87faf473f40fd0317c"),
    "name" : "test 1",
    "sections" : [ 
        {
             "Id" : 1,
             "name" : "first section"
        }, 
        {
            "Id" : 2,
            "name" : "section 2"
        }, 
        {
            "Id" : 3,
            "name" : "section 3"
        }
}

I have tried below

db.collection.aggregate(
[
    {
        "$match": {
            "_id": ObjectId("571c5c87faf473f40fd0317c")
        }
    },
    {
        "$group" : {
            "_id" : "$_id",
            "maxSectionId" : {"$max" : "$sections.Id"}
        }
    }
]);

But instead of returning max int single value it is returning an array of all Ids in sections array.

Further same query when executed in node.js it returns an empty array.

3 Answers 3

13

You can do using simple $project stage

Something like this

db.collection.aggregate([
  { "$project": {
    "maxSectionId": {
      "$arrayElemAt": [
        "$sections",
        {
          "$indexOfArray": [
            "$sections.Id",
            { "$max": "$sections.Id" }
          ]
        }
      ]
    }
  }}
])
Sign up to request clarification or add additional context in comments.

Comments

5

your aggregation query need $unwind for opennig to "sections" array

add your aggregation query this

{$unwind : "$sections"}

and your refactoring aggregation query like this

db.collection.aggregate(
[  
    {$unwind : "$sections"},
    {
        "$match": {
            "_id": ObjectId("571c5c87faf473f40fd0317c")
        }
    },
    {
        "$group" : {
            "_id" : "$_id",
            "maxSectionId" : {"$max" : "$sections.Id"}
        }
    }
]);

and more knowledge for $unwind : https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/

Comments

0

Replace $group with $project

In the $group stage, if the expression resolves to an array, $max does not traverse the array and compares the array as a whole.

With a single expression as its operand, if the expression resolves to an array, $max traverses into the array to operate on the numerical elements of the array to return a single value [sic]

4 Comments

using $project throws exception: invalid operator '$max', code 15999
what version of mongodb are you using... in that case use unwind as told by @kakashi hatake but use it after your match query to limit the number of unwinded documents
MongoDB shell version: 3.0.7. I need max of all unwinded sections but of particular collection._id for which filter is already applied, so can't apply filter on sections.Id.
i meant filter with object id first then unwind like this [{"$match": {"_id": ObjectId("571c5c87faf473f40fd0317c")}},{$unwind:"$sections"}], if you unwind first all the documents irrespective of objectId doc you want will be unwinded first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.