find on binData

576 views
Skip to first unread message

Mike Atkins

unread,
Oct 17, 2011, 4:03:36 PM10/17/11
to mongod...@googlegroups.com
I've been experimenting with using a custom _id field for a gridfs fs.files collection so I can use files_id in fs.chunks as a sharding key. I am using a byte array consisting of two bytes of random data followed by 12 bytes from an ObjectId. Seems to do what I want in terms of sharding, although I am aware that having the _id field randomized can cause a performance hit related to index caching. In any case, I was surprised to see the following (am I not using the right syntax?): 

mongos> db.fs.files.find({}, {_id: 1})
{ "_id" : BinData(0,"lZZOnIbU+ZJkA/7YpAw=") }
{ "_id" : BinData(0,"gpJOnIbX+ZJkAwTZpAw=") }
{ "_id" : BinData(0,"E2NOnIbf+ZJkAwrZpAw=") }
mongos> db.fs.files.find({_id: BinData(0,"lZZOnIbU+ZJkA/7YpAw=") }, {_id: 1})
{ "_id" : BinData(0,"lZZOnIbU+ZJkA/7YpAw=") }
{ "_id" : BinData(0,"gpJOnIbX+ZJkAwTZpAw=") }
{ "_id" : BinData(0,"E2NOnIbf+ZJkAwrZpAw=") }
mongos> db.fs.files.find({_id: BinData(0,"lZZOnIbU+ZJkA/7YpAw=") }, {_id: 1}).count()
0

At the very least the second two results seem inconsistent, although I think both are wrong (I was expecting one document to be returned).
what am I missing?

I'm using version 2.0.0.

Thanks,
-Mike

Mike Atkins

unread,
Oct 18, 2011, 10:13:45 AM10/18/11
to mongod...@googlegroups.com
Looking at this some more, I think this is a bug. A the very least an inconsistency between the way HexData and BinData work. 

> db.bindata.insert({ foo: BinData(0,"gpJOnIbX+ZJkAwTZpAw=")})
> db.bindata.find()
{ "_id" : ObjectId("4e9d87aa5825b60b637815a6"), "foo" : null }

so BinData(0,"gpJOnIbX+ZJkAwTZpAw=")produces null..

> db.bindata.insert({ foo: HexData(0,"5555")})
> db.bindata.find()
{ "_id" : ObjectId("4e9d87aa5825b60b637815a6"), "foo" : null }
{ "_id" : ObjectId("4e9d87d15825b60b637815a7"), "foo" : BinData(0,"VVU=") }

whereas HexData(0, "5555") produces a non-null value, as expected.
If I use the new keyword with BinData, it does what I expect:

> db.bindata.insert({ foo: new BinData(0,"gpJOnIbX+ZJkAwTZpAw=")})
> db.bindata.find()
{ "_id" : ObjectId("4e9d87aa5825b60b637815a6"), "foo" : null }
{ "_id" : ObjectId("4e9d87d15825b60b637815a7"), "foo" : BinData(0,"VVU=") }
{ "_id" : ObjectId("4e9d87e15825b60b637815a8"), "foo" : BinData(0,"gpJOnIbX+ZJkAwTZpAw=") }

and..

> db.bindata.find({foo: new BinData(0,"gpJOnIbX+ZJkAwTZpAw=")})
{ "_id" : ObjectId("4e9d87e15825b60b637815a8"), "foo" : BinData(0,"gpJOnIbX+ZJkAwTZpAw=") }

so, basically "new" needed with BinData, but not HexData.

Mike Atkins

unread,
Oct 18, 2011, 10:23:22 AM10/18/11
to mongod...@googlegroups.com

Mathias Stearn

unread,
Oct 24, 2011, 1:41:45 PM10/24/11
to mongod...@googlegroups.com
Thanks for reporting this. Unfortunately JS semantics can be weird when it comes to constructing objects not implemented in JS, including built-in types like Array. We'll see what we can do to normalize the syntaxes.

Grégoire Seux

unread,
Nov 21, 2011, 6:32:10 AM11/21/11
to mongod...@googlegroups.com
Hello,

this issue while searching with field of type bindata is quite embarassing.

For instance, I search a group of elements with the shard key (_id which is of BinData type).
my query is using $in : [.... array of binDatas]

some of the elements seems to be found and some (always the same number) are not.

How can I do a proper query to make sure to avoid this issue ? (I am using the c# driver 1.3 and mongo 2.0.0)

-- 
Greg

Mathias Stearn

unread,
Nov 21, 2011, 7:12:54 PM11/21/11
to mongod...@googlegroups.com
I think you should be able to query using c#'s binary type (BsonBinaryData). If that isn't working for you make sure you are using the same subtype.

Robert Stam

unread,
Nov 22, 2011, 1:59:27 PM11/22/11
to mongod...@googlegroups.com
Gregoire, not sure what you are asking...

If you could provide a working JSON query in the mongo shell and the same query in your C# program I can verify for you whether they are equivalent.

On Mon, Nov 21, 2011 at 7:12 PM, Mathias Stearn <mat...@10gen.com> wrote:
I think you should be able to query using c#'s binary type (BsonBinaryData). If that isn't working for you make sure you are using the same subtype.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/QKDDXrp4b7IJ.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Reply all
Reply to author
Forward
0 new messages