I think a lot of people have been confused by your terminology of "appending", but if I read between the lines of what you are saying you seem to want the "whole document" to be returned but only including the "matched element" of the array.
So like all general "field selection" techniques, you have to be very specific with MongoDB and include all of the field names in your projection:
db.collection1.find(
{ "array.userid": uId }, // Don't need elemMatch with one field
{
"array.$": 1,
"field1": 1,
"field2": 1,
"field3": 1,
"field4": 1,
"field5": 1,
"field6": 1,
"field7": 1,
"field8": 1,
}
)
The _id
is always included unless explicitly excluded and you cannot "mix" other includes/excludes in here, not that it would make much sense since you need the matched position within the array.
Edit: I have no idea why you would want to do this, to me the above form is much better:
db.collection1.aggregate([
// Filter the matching document, as you do not want to do the rest of the
// steps over the whole collection
{ "$match": {
"array.userid": uId, // Don't need elemMatch with one field
}},
// Store the original document, and a copy of the array
{ "$project": {
"array": 1,
"field1": 1,
"field3": 1,
"field4": 1,
"field5": 1,
"field8": 1,
"dynamicField": "$array"
}},
// Unwind the array
{ "$unwind": "$dynamicField" },
// Just match the field you want
{ "$match": { "dynamicField.userid": uId } },
// Play with the other array now
{ "$unwind": "$array" },
// Change the projection
{ "$project": {
"array.userId": "$array.userId",
"array.subfield1": "$array.subfield1",
"array.subfield4": "$array.subfield4",
"field1": 1,
"field2": 1,
"field3": 1,
"field4": 1,
"field5": 1,
"field6": 1,
"field7": 1,
"field8": 1,
"dynamicField": 1
}},
// Group back the array
{ "$group": {
"_id": "$_id",
"array": { "$push": "$array" },
"field1": { "$first": "$field1" },
"field3": { "$first": "$field3" },
"field4": { "$first": "$field4" },
"field5": { "$first": "$field5" },
"field8": { "$first": "$field8" },
"dynamicField": { "$first": "$dynamicField" }
}}
])