4

I need to insert a document into a collection, which has an ObjectId and a BinData value. Therefore I don't know how to insert it.

With this code I get the error TypeError: Cannot read property 'ObjectId' of undefined.

server/fixtures.js

var ObjectId = Mongo.ObjectID;
var chunk = {
            "_id"     : ObjectId("57a9be3c89c1e4b50c574e3a"),
            "files_id": ObjectId("5113b0062be53b231f9dbc11"),
            "n"       : 0,
            "data"    : BinData(0, "/9j/4AAQSkZJRgA...and...so...on../2Q==")
        };

db.mediafiles.chunks.insert(chunk);

Update

I'm using Meteor

Therefore I can use var ObjectId = Meteor.Collection.ObjectID;. But how do I get the BinData?

ReferenceError: BinData is not defined

6
  • Are you running this from Mongo shell? Commented Aug 9, 2016 at 12:55
  • No. A server-side JS-file Commented Aug 9, 2016 at 12:57
  • Just to be clear before I post my answer, are you using NodeJS (server side JS)? Commented Aug 9, 2016 at 13:18
  • @notionquest I'm using Meteor, which is based on NodeJS. Commented Aug 9, 2016 at 14:12
  • @user3142695 have you found the answer to it? Commented Sep 25, 2020 at 2:04

2 Answers 2

2

Stumbled upon this today as well.

As the other answer mentioned you can use ObjectID and Binary provided by the MongoDB driver. The issue I had was that the binary data was not what I expected after inserting and this is due to the inner workings of the Binary function. It requires either an unencoded string or a buffer, which can be initialized from base64 encoded content like this:

const { Binary, ObjectID } = require('mongodb')

async function run() {
  // Configure MongoDB connection
  const client = new MongoClient()

  // Connect to MongoDB
  await client.connect(...)

  try {
    // Insert data using base64 encoded content and 
    // both ObjectID and Binary from mongodb package
    await client.db().mediafiles.chunks.insert({
      _id: ObjectID('57a9be3c89c1e4b50c574e3a'),
      files_id: ObjectID('5113b0062be53b231f9dbc11'),
      n: 0,
      data: Binary(Buffer.from('/9j/4AAQSkZJRgA...and...so...on../2Q==', 'base64')),
    })
  } finally {
    // Close client if it was opened
    await client.close()
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the NodeJS code to insert data into collection. To answer your question specifically, you need the below statement if you are using NodeJS.

var ObjectId = require('mongodb').ObjectID;

Full NodeJS code (assuming you are using NodeJS):-

var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;

var bindata = new require('mongodb').Binary("ZzEudm1s");

var insertDocument = function(db, callback) {
    var chunk = {
        "_id" : new ObjectId("535e1b88e421ad3a443742e7"),
        "files_id" : new ObjectId("5113b0062be53b231f9dbc11"),
        "n" : 0,
        "data" : bindata
    };

    db.collection('Day1').insertOne(chunk, function(err, result) {
        assert.equal(err, null);
        console.log("Inserted a document into the collection.");
        callback();
    });
};

MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    assert.equal(null, err);
    insertDocument(db, function() {
        db.close();
    });
});

If you need a pure JavaScript object of ObjectId, you can use the below library.

https://www.npmjs.com/package/objectid-purejs

2 Comments

For the ObjectID, I found the info, that I can use Meteor.Collection.ObjectID. But No I get ReferenceError: BinData is not defined
doesn't seem right answer. getting error .Binary is not a function`.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.