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

find_one() returns list of objects instead of one .. when using raw query to find a specific document in an embedded array of documents #104

Open
amr3k opened this issue Feb 12, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@amr3k
Copy link

amr3k commented Feb 12, 2021

Bug

I have a document like this:

{
  "_id": ObjectId("user1"),
  "addresses": [
    {
      "id": ObjectId("a1"),
      "details": [....]
    },
    {
      "id": ObjectId("a2"),
      "details": [....]
    }
  ],
}

I want to get the details of document a1 only.

Current Behavior

When using this code, it returns a document with the full embedded array

address = await engine.find_one(User, {"_id": ObjectId("user1"),
                                        "addresses": {
                                            "$elemMatch": {
                                                "id": ObjectId("a1")
                                            }
                                        }})

It returns:
{ "_id" : ObjectId("user1"), "addresses" : [ { "id" : ObjectId("a1"), "details" : [....]}, { "id" : ObjectId("a2"), "details" : [....]}, .... <rest of array>... ] }

Expected behavior

When I use the mongo shell:
db.users.find({_id: ObjectId("user1")}, {addresses: { $elemMatch: {"id": ObjectId("a1")} }})
It returns:
{ "_id" : ObjectId("user1"), "addresses" : [ { "id" : ObjectId("a1"), "details" : [....]} ] }

Environment

  • ODMantic version: 0.3.2
  • MongoDB version: 4.2.12
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())"): 1.7.3

Additional context

Sorry If I missed something, It's my first time working with NoSQL

@amr3k amr3k added the bug Something isn't working label Feb 12, 2021
@art049
Copy link
Owner

art049 commented Feb 13, 2021

Hi no worries.
Just to confirm, Is it normal that you use both campaigns and addresses ?

@amr3k
Copy link
Author

amr3k commented Feb 13, 2021

No... sorry, my bad.
My original db has campaigns, I only replaced names here for simplifications.
So it's adresses not campaigns.
Apologies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants