0

Lets say this is the data (

{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "ecf", score: 5 } ] } 
{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xya", score: 7 } ] } 
{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 }, { product: "xyz", score: 8 } ] }

I understand that I can make queries like db.inventory.find( { "results.product": "xyz"} ) and get the documents that matches the query. But what I really want is get the subdocuments that fit the criteria.

Example, MY query should return just the below result instead of all the subdocuments in the document. We can assume that i want to do this operation on a single document( i mean, applying _id: 3 explicitly in the query.

{ _id: 3, results: [ { product: "xyz", score: 8 } ] }

UPDATE: I found some answer from some stackoverflow post and based on that, i modified the query accordingly, something like this - db.collection.find({'results.product': "xyz"}, {'results.$' : 1}) this is actually returning only 1 subdocument in results field which matches the criteria

Am i missing something!

UPDATED 2:

{
  _id: 1,
  title: "123 Department Report",
  tags: [ "G", "STLW" ],
  year: 2014,
  subsections: [
    {
      subtitle: "Section 1: Overview",
      content:  "Section 1: This is the content of section 1."
    },
    {
      subtitle: "Section 2.a: Analysis",
      content: "Section 2: This is the content of section 2."
    },
    {
      subtitle: "Section 2.b: Advanced Analysis",
      content: "Section 2: This is the content of section 2."
    },
    {
      subtitle: "Section 3: Budgeting",
      content: "Section 3: This is the content of section3."
    }
  ]
}

the above is a modified version of the example from https://docs.mongodb.com/manual/reference/operator/aggregation/redact/#examples

the query should return the following data, assuming im looking for analysis word in subsections.subtitle field.

{
  _id: 1,
  title: "123 Department Report",
  tags: [ "G", "STLW" ],
  year: 2014,
  subsections: [
    {
      subtitle: "Section 2.a: Analysis",
      content: "Section 2: This is the content of section 2."
    },
    {
      subtitle: "Section 2.b: Advanced Analysis",
      content: "Section 2: This is the content of section 2."
    } 
  ]
}
rrmerugu
  • 1,826
  • 2
  • 20
  • 26
  • You probably looking for the reverse condition, but have a look here(removed just the matching document): https://github.com/rahul-raj/MongoDB/blob/master/mongo-query-1.txt let me know once u find a solution accordingly. – Rahul Raj Mar 24 '18 at 09:00
  • It isnt the solution to ur question, u might want to add some tweak to above query. – Rahul Raj Mar 24 '18 at 09:01
  • hey, i see that you are using $ne , which is a full text match !! but im looking for substring match actually.. sorry the example doesn't speak about substring, but that's what i was looking for – rrmerugu Mar 24 '18 at 10:21
  • Because u have added direct match condition in ur question. Use ´$regex´ for substring match. – Rahul Raj Mar 24 '18 at 10:28
  • @RahulRaj, updated the question, can you look in to it! – rrmerugu Mar 24 '18 at 10:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167460/discussion-between-rrmerugu-and-rahul-raj). – rrmerugu Mar 24 '18 at 10:30
  • The subject line may look same as https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection , but an exact match is used in the mentioned post rather than matching for a pattern. `$regex` cant be used with `cond` operator or in `$filter`. – Rahul Raj Mar 24 '18 at 12:57
  • closest result that I could reach so far: https://github.com/rahul-raj/MongoDB/blob/master/mongo-query-1.txt – Rahul Raj Mar 24 '18 at 13:37
  • @RahulRaj, that works for this scenario much better. – rrmerugu Mar 24 '18 at 14:56
  • @RahulRaj, if you can post the answer here, i will mark it as final answer. – rrmerugu Mar 24 '18 at 15:00
  • cant post as an answer, since this post has been flagged by someone. – Rahul Raj Mar 24 '18 at 15:00

0 Answers0