Skip to content

Commit f811f81

Browse files
committed
2 parents cd8368b + 370460d commit f811f81

File tree

12 files changed

+500
-449
lines changed

12 files changed

+500
-449
lines changed

.github/workflows/github-actions.yml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ on:
1313
tags:
1414
- 'v[0-9]+\.[0-9]+\.[0-9]+*'
1515
env:
16-
MONGODB_3_6: "3.6.23"
17-
MONGODB_4_0: "4.0.28"
18-
MONGODB_4_2: "4.2.25"
1916
MONGODB_4_4: "4.4.29"
2017
MONGODB_5_0: "5.0.31"
21-
MONGODB_6_0: "6.0.20"
22-
MONGODB_7_0: "7.0.17"
23-
MONGODB_8_0: "8.0.5"
18+
MONGODB_6_0: "6.0.22"
19+
MONGODB_7_0: "7.0.19"
20+
MONGODB_8_0: "8.0.9"
2421

2522
PYMONGO_3_12: "3.12.3"
2623
PYMONGO_3_13: "3.13.0"
@@ -36,8 +33,6 @@ env:
3633

3734
MAIN_PYTHON_VERSION: "3.9"
3835

39-
MONGOSH: "2.4.2" # Needed for MongoDB 6.0+
40-
4136
jobs:
4237
linting:
4338
# Run pre-commit (https://pre-commit.com/)
@@ -55,17 +50,14 @@ jobs:
5550
test:
5651
# Test suite run against recent python versions
5752
# and against a few combination of MongoDB and pymongo
58-
runs-on: ubuntu-20.04
53+
runs-on: ubuntu-22.04
5954
strategy:
6055
fail-fast: false
6156
matrix:
6257
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "pypy3.9", "pypy3.10"]
63-
MONGODB: [$MONGODB_4_0]
58+
MONGODB: [$MONGODB_4_4]
6459
PYMONGO: [$PYMONGO_3_12]
6560
include:
66-
- python-version: "3.9"
67-
MONGODB: $MONGODB_3_6
68-
PYMONGO: $PYMONGO_3_12
6961
- python-version: "3.9"
7062
MONGODB: $MONGODB_4_4
7163
PYMONGO: $PYMONGO_3_13
@@ -115,7 +107,6 @@ jobs:
115107
- name: install mongo and ci dependencies
116108
run: |
117109
bash .github/workflows/install_mongo.sh ${{ matrix.MONGODB }}
118-
bash .github/workflows/install_mongosh.sh ${{ matrix.MONGODB }} ${{ env.MONGOSH }}
119110
bash .github/workflows/install_ci_python_dep.sh
120111
bash .github/workflows/start_mongo.sh ${{ matrix.MONGODB }}
121112
- name: tox dry-run (to pre-install venv)

.github/workflows/install_mongo.sh

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,79 @@ set -e # Exit immediately if a command exits with a non-zero status
44
set -u # Treat unset variables as an error
55

66
if [ "$#" -ne 1 ]; then
7-
echo "Usage: $0 <mongodb-version>"
8-
echo "Example: $0 8.0.5"
7+
echo >&2 "Usage: $0 <mongodb-version>"
8+
echo >&2 "Example: $0 8.0.5"
99
exit 1
1010
fi
1111

1212
MONGODB="$1"
13+
MONGOSH=2.5.1
1314

1415
# Determine build name based on version
15-
if [[ "${MONGODB}" =~ ^(7.0|8.0) ]]; then
16-
mongo_build="mongodb-linux-x86_64-ubuntu2004-${MONGODB}"
17-
elif [[ "${MONGODB}" =~ ^(4.0|4.2|4.4|5.0|6.0) ]]; then
18-
mongo_build="mongodb-linux-x86_64-ubuntu1804-${MONGODB}"
19-
elif [[ "${MONGODB}" =~ ^3.6 ]]; then
20-
mongo_build="mongodb-linux-x86_64-${MONGODB}"
16+
if [[ "$MONGODB" =~ ^(6.0|7.0|8.0) ]]; then
17+
mongodb_build="mongodb-linux-x86_64-ubuntu2204-${MONGODB}"
18+
elif [[ "$MONGODB" =~ ^(4.4|5.0) ]]; then
19+
mongodb_build="mongodb-linux-x86_64-ubuntu2004-${MONGODB}"
2120
else
22-
echo "Error: Unsupported MongoDB version: ${MONGODB}"
21+
echo >&2 "Error: Unsupported MongoDB version: ${MONGODB}"
2322
usage
2423
fi
2524

26-
download_url="http://fastdl.mongodb.org/linux/${mongo_build}.tgz"
27-
tarball="${mongo_build}.tgz"
25+
mongodb_tarball="${mongodb_build}.tgz"
26+
mongodb_download_url="http://fastdl.mongodb.org/linux/${mongodb_tarball}"
2827

29-
echo "Downloading MongoDB from ${download_url}..."
30-
if ! wget --quiet "${download_url}"; then
31-
echo "Error: Failed to download MongoDB."
32-
exit 1
33-
fi
28+
mongosh_build="mongosh-${MONGOSH}-linux-x64"
29+
mongosh_tarball="${mongosh_build}.tgz"
30+
mongosh_download_url="https://github.com/mongodb-js/mongosh/releases/download/v${MONGOSH}/${mongosh_tarball}"
31+
32+
set -- \
33+
MongoDB "$mongodb_tarball" "$mongodb_download_url" \
34+
"MongoDB Shell" "$mongosh_tarball" "$mongosh_download_url"
35+
36+
while (( $# > 0 )) ; do
37+
name="$1"
38+
tarball="$2"
39+
download_url="$3"
40+
shift 3
41+
42+
echo >&2 "Downloading ${name} from ${download_url}..."
43+
if ! wget --quiet "$download_url"; then
44+
echo >&2 "Error: Failed to download ${name}."
45+
exit 1
46+
fi
3447

35-
echo "Extracting ${tarball}..."
36-
if ! tar xzf "${tarball}"; then
37-
echo "Error: Failed to extract ${tarball}"
48+
echo >&2 "Extracting ${tarball}..."
49+
if ! tar xzf "${tarball}"; then
50+
echo >&2 "Error: Failed to extract ${tarball}"
51+
exit 1
52+
fi
53+
done
54+
55+
mongodb_dir=$(find "${PWD}/" -type d -name "mongodb-linux-x86_64*" -print -quit)
56+
if [ -z "$mongodb_dir" ]; then
57+
echo >&2 "Error: Could not find MongoDB directory after extraction."
3858
exit 1
3959
fi
4060

41-
mongodb_dir=$(find "${PWD}/" -type d -name "mongodb-linux-x86_64*" | head -n 1)
42-
if [ -z "${mongodb_dir}" ]; then
43-
echo "Error: Could not find MongoDB directory after extraction."
44-
exit 1
61+
mongosh_dir=$(find "${PWD}/" -type d -name "$mongosh_build" -print -quit)
62+
if [ ! -d "$mongosh_dir" ]; then
63+
echo >&2 "Failed to find extracted mongosh directory."
64+
rm -f "$TARBALL"
65+
exit 1
4566
fi
4667

47-
echo "MongoDB installed at: ${mongodb_dir}"
48-
"${mongodb_dir}/bin/mongod" --version
68+
echo >&2 "Creating mongo.path"
69+
echo "export PATH='${mongodb_dir}/bin:${mongosh_dir}/bin:'"'$PATH' \
70+
| tee >&2 mongo.path
71+
72+
. mongo.path
73+
74+
echo >&2 "MongoDB is installed at: ${mongodb_dir}"
75+
mongod >&2 --version
76+
77+
echo >&2 "MongoDB Shell is installed at: ${mongosh_dir}"
78+
mongosh >&2 --version
4979

5080
# Cleanup
51-
echo "Cleaning up..."
52-
rm -f "${tarball}"
81+
echo >&2 "Cleaning up..."
82+
rm -f "$mongodb_tarball" "$mongosh_tarball"

.github/workflows/install_mongosh.sh

Lines changed: 0 additions & 56 deletions
This file was deleted.

.github/workflows/start_mongo.sh

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/bin/bash
22

3+
set -e # Exit immediately if a command exits with a non-zero status
4+
set -u # Treat unset variables as an error
5+
6+
. mongo.path
7+
38
MONGODB=$1
49

5-
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
10+
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*" -print -quit)
611

712
mkdir $mongodb_dir/data
813

@@ -15,12 +20,6 @@ if [ "$MAJOR" -gt 3 ] || ([ "$MAJOR" -eq 3 ] && [ "$MINOR" -ge 8 ]); then
1520
args+=(--setParameter maxTransactionLockRequestTimeoutMillis=1000)
1621
fi
1722

18-
$mongodb_dir/bin/mongod "${args[@]}"
19-
20-
if [ "$MAJOR" -lt 6 ]; then
21-
mongo --verbose --eval "rs.initiate()"
22-
mongo --quiet --eval "rs.status().ok"
23-
else
23+
mongod "${args[@]}"
2424
mongosh --verbose --eval "rs.initiate()"
2525
mongosh --quiet --eval "rs.status().ok"
26-
fi

CONTRIBUTING.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Python3 codebase
2929
Since 0.20, the codebase is exclusively Python 3.
3030

3131
Earlier versions were exclusively Python2, and were relying on 2to3 to support Python3 installs.
32-
Travis runs the tests against the main Python 3.x versions.
32+
Github Actions runs the tests against the main Python 3.x versions.
3333

3434

3535
Style Guide
@@ -65,7 +65,7 @@ You can also run all the checks with ``pre-commit run -a``, this is what is used
6565
Testing
6666
-------
6767

68-
All tests are run on `Travis <http://travis-ci.org/MongoEngine/mongoengine>`_
68+
All tests are run on `Github Actions <https://github.com/MongoEngine/mongoengine/actions>`_
6969
and any pull requests are automatically tested. Any pull requests without
7070
tests will take longer to be integrated and might be refused.
7171

@@ -87,7 +87,7 @@ General Guidelines
8787
- Ensure tests pass on all supported Python, PyMongo, and MongoDB versions.
8888
You can test various Python and PyMongo versions locally by executing
8989
``tox``. For different MongoDB versions, you can rely on our automated
90-
Travis tests.
90+
Github Actions tests.
9191
- Add enhancements or problematic bug fixes to docs/changelog.rst.
9292
- Add yourself to AUTHORS :)
9393

README.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ MongoEngine
66
:Author: Harry Marr (http://github.com/hmarr)
77
:Maintainer: Bastien Gerard (http://github.com/bagerard)
88

9-
.. image:: https://travis-ci.org/MongoEngine/mongoengine.svg?branch=master
10-
:target: https://travis-ci.org/MongoEngine/mongoengine
9+
.. image:: https://github.com/MongoEngine/mongoengine/actions/workflows/github-actions.yml/badge.svg?branch=master
10+
:target: https://github.com/MongoEngine/mongoengine/actions
1111

1212
.. image:: https://coveralls.io/repos/github/MongoEngine/mongoengine/badge.svg?branch=master
1313
:target: https://coveralls.io/github/MongoEngine/mongoengine?branch=master
@@ -35,10 +35,10 @@ an `API reference <https://mongoengine-odm.readthedocs.io/apireference.html>`_.
3535

3636
Supported MongoDB Versions
3737
==========================
38-
MongoEngine is currently tested against MongoDB v3.6, v4.0, v4.4, v5.0, v6.0, v7.0 and v8.0. Future versions
39-
should be supported as well, but aren't actively tested at the moment. Make
40-
sure to open an issue or submit a pull request if you experience any problems
41-
with a more recent MongoDB versions.
38+
MongoEngine is currently tested against MongoDB v4.4, v5.0, v6.0, v7.0 and
39+
v8.0. Future versions should be supported as well, but aren't actively tested
40+
at the moment. Make sure to open an issue or submit a pull request if you
41+
experience any problems with a more recent MongoDB versions.
4242

4343
Installation
4444
============

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
Development
88
===========
99
- (Fill this out as you fix issues and develop your features).
10+
- Fix querying GenericReferenceField with __in operator #2886
1011
- Fix Document.compare_indexes() not working correctly for text indexes on multiple fields #2612
1112
- Add support for transaction through run_in_transaction (kudos to juannyG for this) #2569
1213
Some considerations:

docs/guide/querying.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ Getting related data
511511
When iterating the results of :class:`~mongoengine.fields.ListField` or
512512
:class:`~mongoengine.fields.DictField` we automatically dereference any
513513
:class:`~pymongo.dbref.DBRef` objects as efficiently as possible, reducing the
514-
number the queries to mongo.
514+
number of queries to mongo.
515515

516516
There are times when that efficiency is not enough, documents that have
517517
:class:`~mongoengine.fields.ReferenceField` objects or

mongoengine/queryset/transform.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,15 @@ def query(_doc_cls=None, **kwargs):
129129

130130
singular_ops = [None, "ne", "gt", "gte", "lt", "lte", "not"]
131131
singular_ops += STRING_OPERATORS
132+
is_iterable = False
132133
if op in singular_ops:
133134
value = field.prepare_query_value(op, value)
134135

135136
if isinstance(field, CachedReferenceField) and value:
136137
value = value["_id"]
137138

138139
elif op in ("in", "nin", "all", "near") and not isinstance(value, dict):
140+
is_iterable = True
139141
# Raise an error if the in/nin/all/near param is not iterable.
140142
value = _prepare_query_for_iterable(field, op, value)
141143

@@ -144,10 +146,24 @@ def query(_doc_cls=None, **kwargs):
144146
# * If the value is a DBRef, the key should be "field_name._ref".
145147
# * If the value is an ObjectId, the key should be "field_name._ref.$id".
146148
if isinstance(field, GenericReferenceField):
147-
if isinstance(value, DBRef):
149+
if isinstance(value, DBRef) or (
150+
is_iterable and all(isinstance(v, DBRef) for v in value)
151+
):
148152
parts[-1] += "._ref"
149-
elif isinstance(value, ObjectId):
153+
elif isinstance(value, ObjectId) or (
154+
is_iterable and all(isinstance(v, ObjectId) for v in value)
155+
):
150156
parts[-1] += "._ref.$id"
157+
elif (
158+
is_iterable
159+
and any(isinstance(v, DBRef) for v in value)
160+
and any(isinstance(v, ObjectId) for v in value)
161+
):
162+
raise ValueError(
163+
"The `in`, `nin`, `all`, or `near`-operators cannot "
164+
"be applied to mixed queries of DBRef/ObjectId/%s"
165+
% _doc_cls.__name__
166+
)
151167

152168
# if op and op not in COMPARISON_OPERATORS:
153169
if op:

0 commit comments

Comments
 (0)