odmantic icon indicating copy to clipboard operation
odmantic copied to clipboard

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

Open amr3k opened this issue 3 years ago • 2 comments

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 avatar Feb 12 '21 23:02 amr3k

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

art049 avatar Feb 13 '21 18:02 art049

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

amr3k avatar Feb 13 '21 18:02 amr3k