0

I have subscriptions collection in the database, e.g:

[{
  _id: new ObjectId(...),
  query: {
    size: { $in: [1, 2, 3] }
  }
}, {
  _id: new ObjectId(...),
  query: {
    name: { $eq: 'Shirt' },
  }
}]

I need to find all subscriptions whose query matches the object. Is it possible? E.g:

Subscriptions.aggregate([{
  $reverseMatch: {
    size: 1,
    name: 'Shirt',
  },
}]);
5
  • 1
    It may be me but I'm not sure I understand. Can you share a sample document that and you hope to match or does your document actually have a field named query with a value of { size: { $in: [1, 2, 3] } } ?
    – jQueeny
    Apr 8 at 10:38
  • @jQueeny yes, the document in the database has a field named query with that value. I've updated the question to make it clear.
    – karaxuna
    Apr 8 at 16:11
  • You can do this or this or combined in an $or like this or an exact match like this. Is that the kind of thing your looking for? These can be converted easily to an aggregate with match like this or this
    – jQueeny
    Apr 8 at 16:23
  • @jQueeny yes, but query can be anything, I don't know the structure of query in advance.
    – karaxuna
    Apr 9 at 20:46
  • 1
    I suspect there is some design flaw here. The worst case I can think of is that you need to iterate the whole subscriptions collection's documents one by one in application level and run the query to see if they can match your provided object. Can you better explain what is your use case and what are you trying to do?
    – ray
    Apr 17 at 17:01

1 Answer 1

0
Subscriptions.aggregate([
  {
    $match: {
      $nor: [
        { "query.size": 1 }, // Exclude documents where size is 1
        { "query.size": 2 }, // Exclude documents where size is 2
        { "query.size": 3 }  // Exclude documents where size is 3
      ]
    }
  }
]);

This query effectively filters out subscriptions where the size field inside the query object matches any of the specified values (1, 2, or 3), thereby giving us the subscriptions that do not match the specified object. It's a neat way to handle such filtering requirements in MongoDB aggregation pipelines.

3
  • 1
    Hey, thanks! But this does not answer my question.
    – karaxuna
    Apr 8 at 7:56
  • @karaxuna is there any correlation in the documents in subscriptions collection? Also when you say "find all subscriptions whose query matches the object" is that Query is and condition or OR condition? If you could elaborate more with exact sample records and expected results then problem can be understood and solved. 11 hours ago
  • Considering you have two collections cloths and subscriptions one can think of following mongoplayground.net/p/F9WHoCcLYh7 here first we match the subscription filter and get matched subscription, then do lookup to actual collection for matched subscription then finally filter matched results with required criteria... If you are still thinking of one collection and you are just saving query for subscription then you might need to run the matched queries on target collection and then get the final results. 8 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.