0

Suppose I have the following document in a collection:

{  
   "_id":ObjectId("1123562e7c594c12942f"),
   "figures":[  
      {  
         "shape":"square",
         "color":"blue"
      },
      {  
         "shape":"triangle",
         "color":"black"
      }
   ]
}

I would like to make a query which selects the field "shape" from the second element of the array "figures".

Using db.test.find({}, {"figures": {$slice: [2, 1]}}) you can access the second element of "figures", but can you also select only the field "shape" from there?

2
  • Using .find()? No. You can use the .aggregate() method. But it's better to do this client side. Commented Jan 6, 2016 at 0:34
  • the problem for me of doing it client side is that my actual array entries have fields which are huge, and that's why I don't want to query all the fields Commented Jan 6, 2016 at 1:01

1 Answer 1

1

Use below query

db.test.find({}, {"figures": {$slice: [2, 1]}, "figures.shape": 1}).pretty();
Sign up to request clarification or add additional context in comments.

Comments

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.