Using BinData values in mongo shell

8,102 views
Skip to first unread message

Maxim Kuleshov

unread,
Feb 20, 2012, 8:32:31 AM2/20/12
to mongodb-user
Hi,

How could I use BinData values in mongo shell? If I generate binary
values programmatically - then I see following format upon quering:

db.products.find({},{_id:1})

{ "_id" : BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }
...

But, I can not query particular object by its binary id.

db.products.find({ "_id" :
BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") })

And see not only object with id =
BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv"), but all products I have in
collection.

Also, I tried to create simple object setting its value.

x = {"_id" : BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }

And looks like mongo shell does not recognize it:

{ "_id" : undefined }

Moreover, mongoexport uses another format for binary fields:

{ "_id" : { "$binary" : "AQAAAAEBAAVlbl9VSwAAAAAAAAhv", "$type" :
"00" } }

But it also could not be used in mongo shell.

Scott Hernandez

unread,
Feb 20, 2012, 8:40:02 AM2/20/12
to mongod...@googlegroups.com
What shell version are you using?
> version()

In the 2.0.2/2.1.0 shell this is what I get:
> db.bin.insert({_id: new BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") })
> db.bin.find({_id: new BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") })
{ "_id" : BinData(0,"AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }

All looks fine, no?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> 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.
>

Robert Stam

unread,
Feb 20, 2012, 8:53:28 AM2/20/12
to mongod...@googlegroups.com
On input you have to use "new BinData" instead of just "BinData".

Maxim Kuleshov

unread,
Feb 20, 2012, 10:57:09 AM2/20/12
to mongodb-user
Thank you, guys!

Shame to me, I completely forgot underlying javascript nature of Mongo
and have not been adding "new" while
using BinData.

On 20 фев, 17:53, Robert Stam <rob...@10gen.com> wrote:
> On input you have to use "new BinData" instead of just "BinData".
>
> On Mon, Feb 20, 2012 at 7:40 AM, Scott Hernandez
> <scotthernan...@gmail.com>wrote:

Robert Stam

unread,
Feb 20, 2012, 11:05:25 AM2/20/12
to mongod...@googlegroups.com
I get tripped up by this all the time. Specially since ObjectId is implemented in such a way as to make the "new" optional.

BinData should really behave the same way.

Scott Hernandez

unread,
Feb 20, 2012, 11:27:17 AM2/20/12
to mongod...@googlegroups.com
This has been fixed for the next major release.

In 2.1.0 you can omit the "new" but that is kind of weird in general
(from a javascript/programming point of view) and mostly not a good
practice but something which is supported all the same for the
user-experience.

https://jira.mongodb.org/browse/SERVER-4093

Adil Mazhar

unread,
Sep 5, 2017, 9:18:43 AM9/5/17
to mongodb-user
I'm still facing difficulty uploading small files in mongodb via BinData. Can anyone please share a link for proper documentation of ObjectId and BinData in mongo as i didn't yet find proper documentation anywhere yet? I'm actually confused about the two parameters of BinData() and role of ObjectId for file uploading.

Kevin Adistambha

unread,
Sep 10, 2017, 11:12:09 PM9/10/17
to mongodb-user

Hi,

Could you elaborate on what you’re trying to do? ObjectId and BinData are two different things.

Specifically, ObjectId is a method to allow the creation of a unique document ID, since every MongoDB document requires an _id field as its “primary key”. See ObjectId for the details. Note that ObjectId value by itself is not strictly required by MongoDB. You can create a document containing your own _id field, with the restriction that the value of the field must be unique in a collection. See Field Names for more information.

In contrast, BinData is a specific BSON type, along with strings, numbers, arrays, etc. See BSON Specification Version 1.1. The closest SQL equivalent is a BLOB, although it must observe MongoDB’s BSON 16MB document size limit.

Please note that you are replying to a very old thread. If you have a specific issue, could you create a new post with:

  • Your MongoDB version and your driver version.
  • What is your goal
  • What have you tried so far
  • What error messages you observed

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages