Skip to content

Commit 2628973

Browse files
committed
FIX: issue #76 (Querying indexed field in subdocument does not work, but works without index)
1 parent be47200 commit 2628973

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Diff for: src/main/java/com/github/fakemongo/impl/index/IndexAbstract.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.fakemongo.impl.ExpressionParser;
44
import com.github.fakemongo.impl.Filter;
55
import com.github.fakemongo.impl.Util;
6+
import com.mongodb.BasicDBList;
67
import com.mongodb.DBObject;
78
import com.mongodb.FongoDBCollection;
89
import com.mongodb.MongoException;
@@ -272,7 +273,7 @@ public boolean canHandle(final DBObject queryFields) {
272273
return true;
273274
}
274275

275-
public boolean keyEmbeddedFieldMatch(String field, DBObject queryFields) {
276+
private boolean keyEmbeddedFieldMatch(String field, DBObject queryFields) {
276277
//if field embedded field type
277278
String[] fieldParts = field.split("\\.");
278279
if (fieldParts.length == 0) {
@@ -283,7 +284,10 @@ public boolean keyEmbeddedFieldMatch(String field, DBObject queryFields) {
283284
int count = 0;
284285
for (String fieldPart : fieldParts) {
285286
count++;
286-
if (!searchQueryFields.containsField(fieldPart)) {
287+
if (searchQueryFields instanceof BasicDBList) {
288+
// when it's a list, there's no need to investigate nested documents
289+
return true;
290+
} else if (!searchQueryFields.containsField(fieldPart)) {
287291
return false;
288292
} else if (searchQueryFields.get(fieldPart) instanceof DBObject) {
289293
searchQueryFields = (DBObject) searchQueryFields.get(fieldPart);

Diff for: src/test/java/com/github/fakemongo/FongoTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ public void testFindOneIn() {
174174
assertEquals(new BasicDBObject("date", 1), result);
175175
}
176176

177+
/**
178+
* @see <a href="https://github.com/fakemongo/fongo/issues/76">
179+
* Querying indexed field in subdocument does not work, but works without index
180+
* </a>
181+
*/
182+
@Test
183+
public void testFindOneIn_within_array_and_given_index_set() {
184+
DBCollection collection = newCollection();
185+
// prepare documents
186+
collection.insert(
187+
new BasicDBObject("_id", 1)
188+
.append("animals", new BasicDBObject[]{
189+
new BasicDBObject("name", "dolphin").append("type", "mammal"),
190+
}),
191+
new BasicDBObject("_id", 2)
192+
.append("animals", new BasicDBObject[]{
193+
new BasicDBObject("name", "horse").append("type", "mammal"),
194+
new BasicDBObject("name", "shark").append("type", "fish")
195+
}));
196+
// prepare index
197+
collection.createIndex(new BasicDBObject("animals.type", "text"));
198+
199+
DBObject result = collection.findOne(new BasicDBObject("animals.type", new BasicDBObject("$in", new String[]{"fish"})));
200+
201+
assertNotNull(result);
202+
assertEquals(2, result.get("_id"));
203+
}
204+
177205
@Test
178206
public void testFindOneInWithArray() {
179207
DBCollection collection = newCollection();

0 commit comments

Comments
 (0)