[SERVER-8703] $all does not return document when searching on field named 'query' Created: 25/Feb/13  Updated: 08/Mar/13  Resolved: 25/Feb/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.2.1, 2.2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kay Agahd Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

linux 64 bit


Issue Links:
Duplicate
duplicates SERVER-8025 shell supports "query" works as a met... Closed
Operating System: Linux
Participants:

 Description   

I've created the following document for testing purposes:

{
	"_id" : ObjectId("512b405b20ce3b931d06d9ed"),
	"calls" : [
		{
			"ts" : 123456,
			"n" : 100
		},
		{
			"ts" : 123457,
			"n" : 200
		}
	],
	"query" : [
		"shopId",
		"clusterId",
		"abc3"
	]
}

Using the $all operator finds the document but does not return it:

shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } )

Using $and however returns correctly the document (just to proof that it really exits):

shard01:PRIMARY> db.test.find(  { $and: [ {query:"shopId"}, {query:"clusterId"}, {query:"abc3"} ] }  )
{ "_id" : ObjectId("512b405b20ce3b931d06d9ed"), "calls" : [ { "ts" : 123456, "n" : 100 }, { "ts" : 123457, "n" : 200 } ], "query" : [ "shopId", "clusterId", "abc3" ] }

It seems that $all found the document since n is 1:

shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).explain()
{
	"cursor" : "BtreeCursor query_1",
	"isMultiKey" : true,
	"n" : 1,
	"nscannedObjects" : 5,
	"nscanned" : 5,
	"nscannedObjectsAllPlans" : 5,
	"nscannedAllPlans" : 5,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		"query" : [
			[
				"shopId",
				"shopId"
			]
		]
	},
	"server" : "ws21109:27002"
}

However, find returns nothing and findOne returns null:

shard01:PRIMARY> db.test.findOne( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } )
null
shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).size()
1
shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).count()
1
shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).forEach(function(u){print(u)})
shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).pretty()

However when I add .showDiskLoc, the document is shown correctly:

shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } ).showDiskLoc()
{ "_id" : ObjectId("512b405b20ce3b931d06d9ed"), "calls" : [ { "ts" : 123456, "n" : 100 }, { "ts" : 123457, "n" : 200 } ], "query" : [ "shopId", "clusterId", "abc3" ], "$diskLoc" : { "file" : 0, "offset" : 7162144 } }
shard01:PRIMARY> 

The same happens when queried through the java driver, hasNext will be false.



 Comments   
Comment by Aaron Staple [ 25/Feb/13 ]

Hi agahd,

You should probably consider the field name 'query' reserved in a query for the time being due to SERVER-38 / SERVER-8025 (and avoid using it).

If you need to query on a field named 'query', you can avoid the behavior described here by placing the query field within $and. For example you could change your query above to

{ $and:[ { "query" : { $all : ["shopId", "clusterId", "abc3" ] } } ] }

I'm going to close this ticket as a dup of SERVER-8025. Please feel free to reopen if you have further questions.

Comment by Kay Agahd [ 25/Feb/13 ]

I renamed the field "query" to "array1" which solved my issue:

shard01:PRIMARY> db.test.find( { array1: { $all: [ "shopId", "clusterId", "abc3" ] } } )
{ "_id" : ObjectId("512b405b20ce3b931d06d9ed"), "array1" : [ "shopId", "clusterId", "abc3" ], "calls" : [ { "ts" : 123456, "n" : 100 }, { "ts" : 123457, "n" : 200 } ] }
shard01:PRIMARY> 

Nevertheless, I think it's a bug since "query" is not a reserved field name. If it was the case, mongo should refuse it and alert the user.

Comment by Kay Agahd [ 25/Feb/13 ]

I dropped the index but it did not help either:

shard01:PRIMARY> db.test.find( { "query" : { $all : ["shopId", "clusterId", "abc3" ] } } ).explain()
{
	"cursor" : "BasicCursor",
	"isMultiKey" : false,
	"n" : 1,
	"nscannedObjects" : 16,
	"nscanned" : 16,
	"nscannedObjectsAllPlans" : 16,
	"nscannedAllPlans" : 16,
	"scanAndOrder" : false,
	"indexOnly" : false,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"millis" : 0,
	"indexBounds" : {
		
	},
	"server" : "ws21109:27002"
}
shard01:PRIMARY> db.test.find( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } )
shard01:PRIMARY> db.test.findOne( { query: { $all: [ "shopId", "clusterId", "abc3" ] } } )
null
shard01:PRIMARY> 

Generated at Thu Feb 08 03:18:09 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.