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

396 views
Skip to first unread message

Sam Millman

unread,
Sep 1, 2012, 6:34:32 AM9/1/12
to mongod...@googlegroups.com
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

MONGOALAL

unread,
Sep 1, 2012, 10:41:45 AM9/1/12
to mongod...@googlegroups.com
yes i tried that but it gives only first row and not all matching rows from array
see this


anuj

MONGOALAL

unread,
Sep 2, 2012, 1:03:24 AM9/2/12
to mongod...@googlegroups.com
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:

Stephen Steneker

unread,
Sep 3, 2012, 12:04:54 AM9/3/12
to mongod...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages