0

I have a documents collection with document like this:

{
    _id: ObjectId("5a9d218f9025gerg33759de016"),
    date_created: 2018-03-05 12:53:03.323,
    category: ObjectId("5a83064353113407626f2f67"),
    category_params: [...],
    related_items: Array
        0: ObjectId("5a5ca8ab9025480234633a47"),
        1: ObjectId("5a6ae99f902548125e9c441a"),
        2: ObjectId("5a710ss048912543a0d8bda9"),
        3: ObjectId("5a7ab77b12522243a0d8bee5"),
        [...]
    [...Other fields]
}

I try to do:

'$match': {
    'related_items': {'$in': [related_item_id, ]}
}

But it does not seem to be helping.

I want to aggregate by the ObjectId that is contained in related_items array. e.g. with item #1 in that array (1: ObjectId("5a6ae99f902548125e9c441a"),). How to do that? What $match aggregation should be it? Maybe not $match? I'm doing pymongo aggregation. It does not matter however.

garmoncheg
  • 867
  • 11
  • 26
  • What do you mean by *" I want to aggregate by the ObjectId in that related_items array"*? Aggregate what? It's honestly much easier for people to understand you when you simply show some documents as input and the expected output you want to get from those documents. Not trying to sound mean, but the question mostly reads like you are just spitting out terminology without understanding what any of it really means. Much more straightforward to just show us what you expect to get. – Neil Lunn Apr 17 '18 at 10:32
  • Expect to get a list of documents that have id e.g. ```"5a6ae99f902548125e9c441a"``` in the field ```related_items``` – garmoncheg Apr 17 '18 at 10:36
  • It's a part of big aggregation query with multiple group and other filtering. I don't understand how to do this particular thing. – garmoncheg Apr 17 '18 at 10:37
  • You don't need `$in` here, just specify the value `.find({ "related_items": ObjectId("5a6ae99f902548125e9c441a") })`. You only need `$in` for a "list of values to match" as in `.find({ "related_items": { "$in": [ ObjectId("5a6ae99f902548125e9c441a"), ObjectId("5a7ab77b12522243a0d8bee5")] } })`And this is nothing to do with aggregation. Note if you're writing this in python, you need to import the `ObjectId` function. Using a "plain string" is not a match. – Neil Lunn Apr 17 '18 at 10:45
  • Yes python. I did import ObjectId and wrapped a string. But we can have Array of those _id items in there. Will try. Seems like a clue. – garmoncheg Apr 17 '18 at 10:49
  • It did help Hurray! Thank you. Please pos it like an answer maybe? – garmoncheg Apr 17 '18 at 10:55

0 Answers0