Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_id always blank in embedded documents #184

Open
bdtomlin opened this issue Dec 6, 2021 · 3 comments
Open

_id always blank in embedded documents #184

bdtomlin opened this issue Dec 6, 2021 · 3 comments

Comments

@bdtomlin
Copy link

bdtomlin commented Dec 6, 2021

Using embedded_schema like in the example below always just ends up with a null _id in the database. I would expect it to populate the object id just like it does when using a regular schema.

  @primary_key {:id, :binary_id, autogenerate: true}
  embedded_schema do
    field :name, :string
  end
@scottmessinger
Copy link
Collaborator

@bdtomlin This is definitely surprising. Here's why it's not happening now:

For a root document, the MongoDB server generates the ObjectId and returns it (though you can also insert a document with an _id of your own creation). When you're inserting/updating an embedded document, mongo doesn't see it as a document it needs to assign an _id to for it's own internal indexes. Instead, Mongo just sees an embedded document as an arbitrary map and doesn't add or remove any fields to it. So, in Elixir, when you add an embedded document object into a struct and save the struct, the embedded document is seen by Mongo as a map of arbitrary fields and no field (e.g. _id) is added. If you insert or update an embedded document in an existing documents, the insert/update is implemented as a$set (e.g. {$set: {"author": {name: "Scott"}}}) or a $push/$addToSet (e.g. {$push: {"authors": {"name: "Scott"}}}). Like with insert, Mongo doesn't manipulate those functions to add a _id to the map being inserted/updated.

Looking towards the future, should the adapter try to generate the _id on the client for embedded documents? Seems reasonable! I don't know how that should be implemented but I'd welcome the PR!

@bdtomlin
Copy link
Author

bdtomlin commented Dec 8, 2021

I'm not sure how mongoid (ruby) handles this, but the _id is added by default in embedded documents. I believe there is an option to disable that. I think it should probably be the default here to add the _id as well. It looks like it is for the other mongo libraries.

If I get some time I'll try to look into it. I know elixir-mongo has a function for generating an object id here: https://github.com/elixir-mongo/mongodb/blob/6ae204966e0842c852466431c9fcb06056e1ceb9/lib/mongo.ex#L152-L158

@scottmessinger
Copy link
Collaborator

It would be great if @primary_key {:id, :binary_id, :autogenerate: true} worked for embedded docs! Let me know if you find out how to make it work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants