Skip to content

Commit 97049fe

Browse files
committed
CDRIVER-1975 allow mixed $ and non-$ query ops
1 parent bfa06cc commit 97049fe

File tree

3 files changed

+28
-80
lines changed

3 files changed

+28
-80
lines changed

src/mongoc/mongoc-cursor.c

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -203,34 +203,6 @@ _has_dollar_fields (const bson_t *bson)
203203
}
204204

205205

206-
/* true if there are any non-$ keys. precondition: bson must be valid. */
207-
static bool
208-
_has_nondollar_fields (const bson_t *bson)
209-
{
210-
bson_iter_t iter;
211-
const char *key;
212-
213-
BSON_ASSERT (bson_iter_init (&iter, bson));
214-
while (bson_iter_next (&iter)) {
215-
key = bson_iter_key (&iter);
216-
217-
if (key[0] != '$') {
218-
return true;
219-
}
220-
}
221-
222-
return false;
223-
}
224-
225-
226-
/* true if there are $ and non-$ keys. precondition: bson must be valid. */
227-
static bool
228-
_mixed_dollar_non_dollar (const bson_t *bson)
229-
{
230-
return _has_dollar_fields (bson) && _has_nondollar_fields (bson);
231-
}
232-
233-
234206
#define MARK_FAILED(c) \
235207
do { \
236208
(c)->done = true; \
@@ -412,15 +384,6 @@ _mongoc_cursor_new (mongoc_client_t *client,
412384
bson_append_iter (&opts, key + 1, -1, &iter);
413385
}
414386
}
415-
} else {
416-
/* whole document is query, like "{a: 1}" or "{$or: [...]}" */
417-
if (_mixed_dollar_non_dollar (query)) {
418-
bson_set_error (&error, MONGOC_ERROR_CURSOR,
419-
MONGOC_ERROR_CURSOR_INVALID_CURSOR,
420-
"Cannot mix top-level query with dollar keys such "
421-
"as $orderby. Use {$query: {},...} instead.");
422-
GOTO (done);
423-
}
424387
}
425388
}
426389

@@ -667,7 +630,7 @@ _mongoc_cursor_monitor_legacy_query (mongoc_cursor_t *cursor,
667630
/* successful */
668631
RETURN (true);
669632
}
670-
633+
671634
bson_init (&doc);
672635
bson_strncpy (db, cursor->ns, cursor->dblen + 1);
673636

tests/test-mongoc-collection-find.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,22 @@ test_dollar_or (void)
413413
}
414414

415415

416+
static void
417+
test_mixed_dollar_nondollar (void)
418+
{
419+
test_collection_find_t test_data = TEST_COLLECTION_FIND_INIT;
420+
421+
test_data.docs = "[{'a': 1}, {'a': 1, 'b': 2}, {'a': 2}]";
422+
test_data.query_input = "{'a': 1, '$or': [{'b': 1}, {'b': 2}]}";
423+
test_data.expected_op_query = test_data.query_input;
424+
test_data.expected_find_command =
425+
"{'find': 'collection', 'filter': {'a': 1, '$or': [{'b': 1}, {'b': 2}]}}";
426+
427+
test_data.expected_result = "[{'a': 1, 'b': 2}]";
428+
_test_collection_find (&test_data);
429+
}
430+
431+
416432
/* test that we can query for a document by a key named "filter" */
417433
static void
418434
test_key_named_filter (void)
@@ -1085,7 +1101,7 @@ _test_tailable_timeout (bool pooled)
10851101
} else {
10861102
client = test_framework_client_new ();
10871103
}
1088-
1104+
10891105
database = mongoc_client_get_database (client, "test");
10901106
collection_name = gen_collection_name ("test");
10911107

@@ -1123,7 +1139,7 @@ _test_tailable_timeout (bool pooled)
11231139
mongoc_collection_destroy (collection);
11241140
mongoc_database_destroy (database);
11251141
bson_free (collection_name);
1126-
1142+
11271143
if (pooled) {
11281144
mongoc_client_pool_push (pool, client);
11291145
mongoc_client_pool_destroy (pool);
@@ -1152,13 +1168,15 @@ test_tailable_timeout_pooled (void)
11521168
void
11531169
test_collection_find_install (TestSuite *suite)
11541170
{
1155-
TestSuite_AddLive (suite, "/Collection/find/dollar_query",
1156-
test_dollar_query);
1157-
TestSuite_AddLive (suite, "/Collection/find/dollar_or",
1158-
test_dollar_or);
1159-
TestSuite_AddLive (suite, "/Collection/find/key_named_filter",
1160-
test_key_named_filter);
1161-
TestSuite_AddLive (suite, "/Collection/find/key_named_filter/$query",
1171+
TestSuite_AddLive (
1172+
suite, "/Collection/find/dollar_query", test_dollar_query);
1173+
TestSuite_AddLive (suite, "/Collection/find/dollar_or", test_dollar_or);
1174+
TestSuite_AddLive (suite,
1175+
"/Collection/find/mixed_dollar_nondollar",
1176+
test_mixed_dollar_nondollar);
1177+
TestSuite_AddLive (suite, "/Collection/find/key_named_filter", test_key_named_filter);
1178+
TestSuite_AddLive (suite,
1179+
"/Collection/find/key_named_filter/$query",
11621180
test_key_named_filter_with_dollar_query);
11631181
TestSuite_AddLive (suite, "/Collection/find/subdoc_named_filter",
11641182
test_subdoc_named_filter);

tests/test-mongoc-cursor.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -122,38 +122,6 @@ test_clone (void)
122122
}
123123

124124

125-
static void
126-
test_invalid_query (void)
127-
{
128-
mongoc_client_t *client;
129-
mongoc_cursor_t *cursor;
130-
bson_error_t error;
131-
const bson_t *doc = NULL;
132-
bson_t *q;
133-
bool r;
134-
135-
client = test_framework_client_new ();
136-
assert (client);
137-
138-
q = BCON_NEW ("foo", BCON_INT32 (1), "$orderby", "{", "}");
139-
140-
cursor = _mongoc_cursor_new (client, "test.test", MONGOC_QUERY_NONE, 0, 1, 1,
141-
false, q, NULL, NULL, NULL);
142-
assert (!mongoc_cursor_is_alive (cursor));
143-
r = mongoc_cursor_next (cursor, &doc);
144-
assert (!r);
145-
mongoc_cursor_error (cursor, &error);
146-
assert (strstr (error.message, "$query"));
147-
assert (error.domain == MONGOC_ERROR_CURSOR);
148-
assert (error.code == MONGOC_ERROR_CURSOR_INVALID_CURSOR);
149-
assert (doc == NULL);
150-
151-
bson_destroy (q);
152-
mongoc_cursor_destroy (cursor);
153-
mongoc_client_destroy (client);
154-
}
155-
156-
157125
static void
158126
test_limit (void)
159127
{
@@ -1514,7 +1482,6 @@ test_cursor_install (TestSuite *suite)
15141482
{
15151483
TestSuite_AddLive (suite, "/Cursor/get_host", test_get_host);
15161484
TestSuite_AddLive (suite, "/Cursor/clone", test_clone);
1517-
TestSuite_AddLive (suite, "/Cursor/invalid_query", test_invalid_query);
15181485
TestSuite_AddLive (suite, "/Cursor/limit", test_limit);
15191486
TestSuite_AddLive (suite, "/Cursor/kill/live", test_kill_cursor_live);
15201487
TestSuite_Add (suite, "/Cursor/kill/single", test_kill_cursors_single);

0 commit comments

Comments
 (0)