Re: [mongodb-user] selecting part of array within document: is thsi still the restriction in 2.2 or 2.3?

Showing 1-4 of 4 messages
Re: [mongodb-user] selecting part of array within document: is thsi still the restriction in 2.2 or 2.3? Sammaye 9/1/12 3:34 AM
This should be possible with the positonal operator return in 2.2: https://jira.mongodb.org/browse/SERVER-1243

On 1 September 2012 01:37, MONGOALAL <anuj...@gmail.com> wrote:


This was raised earlier in 2010 and wondering whether this restriction is still there? I'd like to avoid doing this by mapreduce.

Appreciate quick reply from gurus

http://stackoverflow.com/questions/3985214/mongodb-extract-only-the-selected-item-in-array

Suppose you have the following:

// Document 1
{ "shapes" : [
  {"shape" : "square", "color" : "blue"},
  {"shape" : "circle","color" : "red"}
  ] 
}


// Document 2
{ "shapes" : [
  {"shape" : "square",  "color" : "black"},
  {"shape" : "circle",  "color" : "green"}
  ] 
}

do query:

 db.test.find({"shapes.color":"red"}, {"shapes.color":1})

or

db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color":1})

return document (Document 1) matched, but with always ALL array items in shapes:

{ "shapes" : [
  {"shape" : "square", "color" : "blue"},
  {"shape" : "circle","color" : "red"}
  ] 
}

I just want document (Document 1) filtered with ONLY array item color=red:

{ "shapes" : [
  {"shape" : "circle","color" : "red"}
  ] 
}

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Re: [mongodb-user] selecting part of array within document: is thsi still the restriction in 2.2 or 2.3? MONGOALAL 9/1/12 7:41 AM
yes i tried that but it gives only first row and not all matching rows from array
see this


anuj
Re: [mongodb-user] selecting part of array within document: is thsi still the restriction in 2.2 or 2.3? MONGOALAL 9/1/12 10:03 PM
can someone clarify returning first matching row only is intended functionality or bug?
Based on doc, it appears to be bug.
Appreciate if some one can clarify


On Saturday, September 1, 2012 3:34:40 AM UTC-7, Sammaye wrote:
Re: [mongodb-user] selecting part of array within document: is thsi still the restriction in 2.2 or 2.3? Stephen Steneker 9/2/12 9:04 PM
can someone clarify returning first matching row only is intended functionality or bug?
Based on doc, it appears to be bug.
Appreciate if some one can clarify

Hi,

The positional operator currently only returns the first match found per document.

If you want to manipulate arrays as separate documents, you need to reshape your output using Map/Reduce or the new Aggregation Framework (MongoDB 2.2).

With the Aggregation Framework you can use $unwind to separate an array into a stream of documents, and then use other operators like $match or $group as appropriate to filter and recombine.

I've added an example to the Stack Overflow post you referenced:

Cheers,
Stephen