MongoEngine / MongoEngine/mongoengine

Can't use $ in fields projection on queryset for selecting array entries

Open
#1,997 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

The following pymongo query can't be recreated with the MongoEngine QuerySet.

customers = db.customers.find({ _id: ObjectId('...'), 'tasks.id': 'task_1' }, { 'tasks.$': 1 })

This query tries to find a document in the customers collection with a given id and a task with the id 'task_1' in the tasks array on the customer document. The projection will only return the first task in that array that matches the query { 'id': 'task_1' }. This is useful for when you need a specific item in an array and it would be easier to fetch the document with only the single array item in the tasks array than fetch the customer document and filter the tasks in the code.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test entry point is named. Start by locating MongoEngine QuerySet projection handling and compare it with the shown PyMongo query; done means an equivalent projection can select the first matching array entry and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.