odmantic
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
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
Hi no worries.
Just to confirm, Is it normal that you use both campaigns
and addresses
?
No... sorry, my bad.
My original db has campaigns
, I only replaced names here for simplifications.
So it's adresses
not campaigns
.
Apologies.