I'm trying to not only reference another document in another collection (that can be easily done with a reference type and an id), I would also like to embed a document from another collection – or even an array of them. However, this document (or these documents) can be shared across multiple documents of the main collection. Technically, the other collection isn't really needed. It just serves as a reference. But without it I wouldn't even know where to start specifying this.
I was thinking this could be done by a valueAggregator but the examples in the main readme have me stumped: The countAggregator seems to look in the first collection for documents where its field1 equals the _id of this document and counts those where this count is stored in the count field`.
"count": {
"type": "countAggregator",
"database": "test",
"collection": "first",
"query": {
"field1": "$$_id"
}
}
Both the boundAggregator and the valueAggregator seem to work similarly but the examples don't seem to be right (besides a missing , in the _id definition):
"count": {
"type": "valueAggregator",
"database": "test",
"collection": "first",
"key": "field2",
"values": {
"field1": "$$_id"
}
}
A key is not recognized, count should be values and values should be query, right?
Here is the example from the ReadMe: https://mongoplayground.net/p/Yw44LP653zJ
Here is the one with my corrections: https://mongoplayground.net/p/eqLPJ1vWpzY
And indeed, this is much more like the full-aggregation example.
However, they're all using $$_id. Is it possible to use another field of the current collection?
I'm trying it here with an aID field that pseudogenerates the reference to an _id of a document of collection a inside a document of collection b but the field a which uses it is always empty – maybe aID doesn't get populated before a does?
What is the best approach? Generate a list of b IDs in a?
Of course, I could just generate a whole object in b that looks like a but those document mustn't be unique.
I'm trying to not only reference another document in another collection (that can be easily done with a
referencetype and an id), I would also like to embed a document from another collection – or even an array of them. However, this document (or these documents) can be shared across multiple documents of the main collection. Technically, the other collection isn't really needed. It just serves as a reference. But without it I wouldn't even know where to start specifying this.I was thinking this could be done by a
valueAggregatorbut the examples in the main readme have me stumped: ThecountAggregatorseems to look in thefirstcollection for documents where itsfield1equals the_idof this document and counts those where this count is stored in thecountfield`.Both the
boundAggregatorand thevalueAggregatorseem to work similarly but the examples don't seem to be right (besides a missing,in the_iddefinition):A
keyis not recognized,countshould bevaluesandvaluesshould bequery, right?Here is the example from the ReadMe: https://mongoplayground.net/p/Yw44LP653zJ
Here is the one with my corrections: https://mongoplayground.net/p/eqLPJ1vWpzY
And indeed, this is much more like the
full-aggregationexample.However, they're all using
$$_id. Is it possible to use another field of the current collection?I'm trying it here with an
aIDfield that pseudogenerates the reference to an_idof a document of collectionainside a document of collectionbbut the fieldawhich uses it is always empty – maybeaIDdoesn't get populated beforeadoes?What is the best approach? Generate a list of
bIDs ina?Of course, I could just generate a whole object in
bthat looks likeabut those document mustn't be unique.