Aggregate - return the whole array if query match one element in the array

Retrieve by RevisionNum but return all array element.

{
_id: ObjectId(‘613f16eda156d84fd428510f’),
Templates: [
{
RevisionNum: “210719”,
EffectiveDate: 2021-07-19T17:43:06.693+00:00,
HardwareVer: “A”,
SoftwareVer: “1.0”,
WorkTasks: [ “ObjectId(‘60f5b97d60762e6a10002820’)”, “ObjectId(‘60f5b98760762e6a10002822’)” ],
HasTraveller: true
},
{
RevisionNum: “220104”,
EffectiveDate: 2022-04-01T17:43:06.693+00:00, HardwareVer: “B”,
SoftwareVer: “1.5”,
WorkTasks: [ “ObjectId(‘60f5b97d60762e6a10002820’)” ],
HasTraveller: false
}
}
I use the query method

MainAssyTemplate.aggregate([
{
$match: {
// _id: req.query.productId,
“Templates.RevisionNum”: “220104”,
},
},

or the same I had try that

MainAssyTemplate.aggregate([
{
$match: {
// _id: req.query.productId,
Templates: { $elemMatch: { RevisionNum: “220104” } },
},
},

How can retrieve another way to get one templates element collection.?

Please read Formatting code and log snippets in posts and update your sample documents so that we can cut-n-paste into our system.

This is the third time I write you the above. Help us help you by providing your document in a usable form. Editing documents that are badly formatted is time consuming. It is easier to help others that have well formatted documents so we answer them faster. It is really really easy for you to supply documents that are easy to cut-n-paste into our systems.

Off-topic: Maybe there should be more users allowed to edit posts, or even some of the staff.

Also most of these users can’t really un-notice the markdown issues, (I mean, if you can write 2 lines of code…) so I’d put the posts “on hold” until they are ready to be answered.

May be a topic worth discussing at a higher level, by moderators or employees.

( Have a nice 24th! :slight_smile: )

1 Like

NO. People have to learn how to use their tools otherwise they keep repeating the same pattern. Just like @Min_Thein_Win just did in Retrieve data by date and time inside of array element.

Doing it for them will just feed them for the day.

2 Likes

True. And some kind of putting the post on hold and alert the user ?

I see that devs at SO or other pop forums dont have much of a better solution…