Skip to content

Embedded collections #106

Description

@FUHDB

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions