5
{
    "_id" : ObjectId("5e3179e83708dc0dcfefaf"),

    "CaseNumber" : "T978045628",

    "ServiceReferences" : [ 

        ObjectId("5e317d48bc13eaf17712a786"),

        ObjectId("5e317f0ec08d57f4a88c3444"),

        ObjectId("5e317f0ec08d57f4a88c3234")

    ]

}

I have a collection with above fields. I want to project the 'ServiceReferences' array except first element as I want to skip first indexed element of the array. Please help me. I have tried a couple of use cases like :

db.getCollection('cases').aggregate([{
             "$project": {

                  "ServRefs": { $pop: { ServiceReferences: -1 } }
              }
       },
       {
        $lookup: {

            from: 'services',

            localField: 'ServRefs',

            foreignField: '_id',

            as: 'serviceDoc'

        }
    }, { $unwind: '$serviceDoc' }])
1
  • Can you please give an example of the expected result? Commented Feb 5, 2020 at 11:36

3 Answers 3

7

Just try with this one

db.getCollection('cases').aggregate([
{ $project: { ServiceReferences: 1, SerRef: { $slice: [ "$ServiceReferences", 1, {$subtract: [ {$size:"$ServiceReferences"}, 1 ]} ] }} }
])
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. It is working fine... First I checked the existence of the length then it worked :: { "$match": { "ServicesReferences.1":{$exists:true}} }, Otherwise it will through :: errmsg" : "Third argument to $slice must be positive: 0
Good. It's better to check the existence.
It might be more convenient omitting the $subtract operation. Just { $slice: [ "$ServiceReferences", 1, {$size:"$ServiceReferences"}]}.
0

You can remove the first element of an array by using $pop

The $pop operator removes the first or last element of an array. Pass $pop a value of -1 to remove the first element of an array and 1 to remove the last element in an array.

You can apply it to your projection.

Here is the documentaiton: $pop documentation mongoDb

3 Comments

I cann't remove first element from array because I need it in the next projection which is in the same aggregate query. I just want to ignore the first element.
I want to Project all items in an array except first one, to new field which is exact opposite of this query :: { "$project": { "ServRef" : { "$arrayElemAt" : ["$ServicesReferences", 0] } } }
$pop can't be used in an aggregation.
0

use slice

https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/

The following syntax returns elements from the specified position in the array:

db.in_house_beta.aggregate([ 
     { $project: { "experience_combined": { $slice: [ "$experience_combined", 1 ] } } },
     { $unwind: "$experience_combined" },
     { $unwind: "$experience_combined.Company" },
     { $group:{ _id:{ Company:'$experience_combined.Company'}, count: { $sum:1} } 
}])

1 Comment

{ $slice: [ "$experience_combined", 1 ] } means 1 element from position 0. If $experience_combined equals [1,2,3,4], { $slice: [ "$experience_combined", 1 ] } gives [1]

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.