0

I have a collection in my MongoDB and have data in it. I need to write a query that fetches a specific field based on the condition from an array of objects in my collection.

collection each row contains :

mouseClicks :[
  {
   "url":"abc.com",
   "moves":{},
   "sitedata":""
  },
  {
 ....
 ....
  } ,...
]

I want to fetch sitedata field that matches the specified url.

mongodb version : 4.2.0

db.log5.find({'mouseClicks':{$elemMatch :{'url':"first.html"}}},{'mouseClicks.sitedata':1} )

I got all the sitedata values from all the documents which are not matching the condition. I expect the output is to fetch only the sitedata for the corresponding url.

  • FYI. You're very own choice of question titled entered as a [Google search produces that exact same question and answers as the very top search result](https://www.google.com/search?q=How+to+form+mongo+query+to+fetch+specific+field+from+array+of+objects+that+match+a+conditio). – Neil Lunn Oct 17 '19 at 10:52
  • @NeilLunn ,yes it is .but i want only the specified field(sitedata) from the document .That's what i expect . – Gomathimeena Subburayan Oct 17 '19 at 11:39

0 Answers0