Sorry I tried to make the sample as simple as I can think of and I see
where I am missing an input :P, should be:
function( previousID, newID )
{
db.col.find(
{ "SUBS._id" : previousID } ).forEach(
function( thing )
{
previousValue = thing.SUBS.$.VALUE;
db.col.update(
{ "_id" : thing._id },
{ $push : { "SUBS" :
{ "_id" : newID,
"VALUE" : previousValue * 5 } } } );
} );
}
The primary purpose of the query is to determine if a primary document
contains the subdocument having SUBS._id:previousID within its array/
list of SUBS. If it does contain the subdocument, I need to use some
of its values to generate a new subdocument to store in the same
primary document. Note that there can be many primary documents
containing this particular previousID, thus I need to $push this new
subdocument to each of those primary documents' SUBS array/list. I am
sorry the example does not look like it fits any real use case, but I
cannot disclose any specifics about the actual use case. What is
needed is to:
A.) Index a subdocument contained within an array of the primary
document (Mongo can handle this)
B.) Be able to query for the primary document based on its
subdocuments fields (No problem here)
C.) I need to know which subdocument was matched, thus which satisfied
the query expression (Sadly, I cannot see how this is possible in
Mongo)
D.) Lastly I need to retrieve data from the subdocument that satisfied
the query (I do not see a way to do this either)
The only work around I can think of is to de-normalize my schema and
move the subdocuments out into their own collection. This brings be
back to a simple row structure. The problem I have is that this de-
normalization defeats the purpose of using the document oriented
benefits of Mongo. This is an area I see lacking most in Mongo. It
may be fast, but we are limited mostly to manipulation of the primary
document, but it is challenged when trying to retrieve, access and
manipulate its subdocuments.
The de-normalized approach is also less efficient since uniqueness is
defined by primary._id and SUBS._id and thus I would have to include
another ID value that ensure uniqueness, but serves no purpose in the
design. This table will get extremely large and keeping it light as
possible is critical.
I figured this was possible since Mongo offers the $ operator and I
would be able to update SUBS.$.VALUE, but in my case I do not want it
updated I want to know its value so I can use it to update/create
something else.
Cheers
On May 12, 5:35 am, Scott Hernandez <
scotthernan...@gmail.com> wrote:
> Your code and syntax is perplexing. There is a feature request to allow
> expressions in the update statement; I can't say how long until it is
> implemented.
>
> Here is one related issue with doing a query, but the update you are talking
> about would be very similar in my mind:
http://jira.mongodb.org/browse/SERVER-2549
>
> Feel free to add a jira issue for your exact needs and/or provide a patch.
http://jira.mongodb.org