0

I am using mongodb + node for rendering pages

I have a mongodb collection with data like Below,

I want the data which is under Mark and which is under the date 2019-06-21

How to match the field with the field which is inside array?

Both condition should match . can someone guide me How to give condition for this collection ?

_id:5d0c9f38082276944579ba3e
user_Name:"Mark"
rows:[
{
   project_ID:"xxxx"
   issue_Summary:"aaaa"
   short_Description:"aaaa"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
   _id:5d0ca14e138a7628948804af
   date:2019-06-21 05:30:00.0001
}
{
   project_ID:"yyyy"
   issue_Summary:"bbb"
   short_Description:"bbb"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
  _id:5d0ca14e138a7628948804af
  date:2019-06-21 05:30:00.0001
}
]

 _id:5d0c9f38082276944579ba3e
user_Name:"Mark"
rows:[
{
   project_ID:"xxxx"
   issue_Summary:"aaaa"
   short_Description:"aaaa"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
   _id:5d0ca14e138a7628948804af
   date:2019-06-22 05:30:00.0001
}
{
   project_ID:"yyyy"
   issue_Summary:"bbb"
   short_Description:"bbb"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
  _id:5d0ca14e138a7628948804af
  date:2019-06-22 05:30:00.0001
}
]

_id:5d0c9f38082276944579ba3e
user_Name:"Dany"
rows:[
{
   project_ID:"xyz"
   issue_Summary:"aaaa"
   short_Description:"aaaa"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
   _id:5d0ca14e138a7628948804af
   date:2019-06-21 05:30:00.0001
}
{
   project_ID:"yzx"
   issue_Summary:"bbb"
   short_Description:"bbb"
   start_Time:"02:02"
   end_Time:"03:02"
   total_Time:""
  _id:5d0ca14e138a7628948804af
  date:2019-06-21 05:30:00.0001
}
]

It saves separate collection for each particular Date

Wolverine
  • 17
  • 7
  • Possible duplicate of [How to filter array in subdocument with MongoDB](https://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb) – Vikash_Singh Jun 26 '19 at 08:47
  • https://stackoverflow.com/questions/36229123/return-only-matched-sub-document-elements-within-a-nested-array – Vikash_Singh Jun 26 '19 at 08:48
  • @Vikash Singh Those two questions explains how to filter inside array, But I want to match the field which is outside array with the field which is inside array. I want to check for the particular **user_Name** (which is not inside in the array) with particular **date** – Wolverine Jun 26 '19 at 09:19

2 Answers2

0

you can try by using mongodb projection elemMatch docs.mongodb.com/manual/reference/operator/projection/elemMatch

  • I have tried using $elemMatch but it is not taking the exact condition. **_data.find(model,({ user_Name:templateData.user.user_id },{ rows: { $elemMatch: { date: data.payload.date } } })).then(function(result) { if(result) }** It's not taking user_Name for match, It's taking only the date – Wolverine Jun 26 '19 at 08:42
0

Also, you can make combination with "and" to match the username and date together.

db.collection.find({$and:[{user_Name:templateData.user.user_id} ,{rows:{$elemMatch: {date:data.payload.date}}}]})
het
  • 781
  • 9
  • 16