1

I have items like these in my collection

{
user: data,
somestuff: [somedata ...], 
terminals: [ {
   label: data,
   details: [{more content}, {}, ...]
   }]
}

I would use 'find' to extract "details" field for a specific terminal 'label' I know that there is an easy way to get the "terminals" array by:

collection.find({_id: "..."}, {_id:0, terminals: 1})

Wich return

{ terminals: [ {
   label: data,
   details: [{more content}, {}, ...]
   }]
}

I tried

collection.find({ "terminals.label": data }, { _id: 0, "terminals.$.details": 1 })

As edirican has suggested And it almost work, but it return the same structure than previously except that the terminals list contain only the labeled document

The result I expect is the details list, extracted from terminals

{ details: [{more content}, {}, ...] }

Thanks for your help !

1
  • It's not clear what your looking for. Can you update your question with an example of the result you're expecting? Commented Jul 22, 2014 at 21:30

1 Answer 1

1

Use positional ($) projection.

http://docs.mongodb.org/manual/reference/operator/projection/positional/

collection.find({ "terminals.label": 2 }, { _id: 0, "terminals.$.details": 1 })
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.