How do I call Mongo ObjectID's from Meteor?

I imported about 154 documents from a CSV file using the mongoimport and I realized that it generated ObjectID’s for each document. Now in my Meteor app I need to update specific documents, but I can’t for the life of me figure out why I keep getting this error:
Error: After filtering out keys not in the schema, your modifier is now empty
W20160514-22:27:52.149(-5)? (STDERR) at [object Object].doValidate (packages/aldeed_collection2-core/lib/collection2.js:374:1)
W20160514-22:27:52.149(-5)? (STDERR) at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core/lib/collection2.js:173:1)
W20160514-22:27:52.149(-5)? (STDERR) at Function. (server/modules/generate-accounts.js:64:14)
W20160514-22:27:52.149(-5)? (STDERR) at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1022:16)
W20160514-22:27:52.149(-5)? (STDERR) at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:869:44)
W20160514-22:27:52.149(-5)? (STDERR) at Object.generateAccountsBFBL (server/modules/generate-accounts.js:21:16)
W20160514-22:27:52.150(-5)? (STDERR) at _generateAccountsBFBL (server/modules/startup.js:20:50)
W20160514-22:27:52.150(-5)? (STDERR) at Object.startup (server/modules/startup.js:1:21)
W20160514-22:27:52.150(-5)? (STDERR) at server/startup.js:1:38
W20160514-22:27:52.150(-5)? (STDERR) at /Users/adriangracia/com223/ilstuwards_admin_portal/.meteor/local/build/programs/server/boot.js:290:5

I get that when I try to do the following
`
var bfblCursor = BFBL.find({});
bfblCursor.forEach(function(member){

  //run through bfbl collection and create users based off name/email
  var name = member.business + member.name + member.city + '_' + member.category;
  var username = name.split(' ').join('_');
  let usernameExists = _checkIfUsernameExists (username);
  let emailExists = _checkIfUserEmailExists( member.email );
  let multEmails = member.email.split(' ')
  console.log('email' + member.email); 

  if( !usernameExists ){
    let userID = null;
    if(!emailExists){
      userId = Accounts.createUser({ 
      username: username, 
      email: member.email,
      password: 'password',
      profile: {

     },
    }); 
    }
    else if( emailExists ){
      console.log("email exists:" + member.email);

      userId = Accounts.createUser({ 
      username: username, 
      password: 'password',
      profile: {

     },
    });
    }
    Meteor.users.update( userId  , { $set: {
      isVerified: true}
    });
    console.log("User added:" + username);

    //var oid = new Meteor.Collection.ObjectID(member._id.toString());

    //var d = BFBL.find(member._id.toString());
    //console.log(d);

    BFBL.update( member._id.toString() , {
      $set: {
        'user': userId
      }
    }, function(error){
      if(error) throw error;
    });
    ` 

It’s really annoying me and I need to do it this way because I need that userID to work as a foreign key for the BFBL collection!

It is not working because the _id for each document is saved with an ObjectID("…") and it is difficult to find a specific id. I feel like I have tried everything. Thanks.

Maybe there is something wrong with the way you generate id’s?
const generatedId = new Mongo.ObjectID()._str

1 Like

I think that the mongodb automatically generated the id’s when I did the mongoimport. When I check the database it’ll say that each document has a _id :Object("…")
I can grab the id number of each document inside the forEach (as a string) when I look through the cursor, but in terms of updating a specific document it won’t let me match the id’s.

With the help of some stack overflow I changed the update call to this:

BFBL.update( { '_id': new Meteor.Collection.ObjectID(member._id) }, { $set: { 'user': userId } }, function(error){ if(error) throw error; });

And now my error is this!

W20160515-12:48:02.492(-5)? (STDERR) throw(ex); W20160515-12:48:02.492(-5)? (STDERR) ^ W20160515-12:48:02.664(-5)? (STDERR) TypeError: Object ObjectID("572290a10fb21ff6a2d1fd68") has no method 'toLowerCase' W20160515-12:48:02.664(-5)? (STDERR) at new MongoID.ObjectID (packages/mongo-id/id.js:11:1) W20160515-12:48:02.664(-5)? (STDERR) at Function.<anonymous> (server/modules/generate-accounts.js:65:22) W20160515-12:48:02.664(-5)? (STDERR) at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1022:16) W20160515-12:48:02.664(-5)? (STDERR) at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:869:44) W20160515-12:48:02.665(-5)? (STDERR) at Object.generateAccountsBFBL (server/modules/generate-accounts.js:22:16) W20160515-12:48:02.665(-5)? (STDERR) at _generateAccountsBFBL (server/modules/startup.js:20:50) W20160515-12:48:02.665(-5)? (STDERR) at Object.startup (server/modules/startup.js:1:21) W20160515-12:48:02.665(-5)? (STDERR) at server/startup.js:1:38 W20160515-12:48:02.665(-5)? (STDERR) at /Users/adriangracia/com223/ilstuwards_admin_portal/.meteor/local/build/programs/server/boot.js:290:5

Not really sure why its screaming at me about now having a lowercase function. Like where does that even come from?

It should be

BFBL.update( { '_id': Meteor.Collection.ObjectID() }, {
$set: {
'user': userId
}
}, function(error){
if(error) throw error;
});

Just tried that and I got this error:

W20160515-13:38:19.760(-5)? (STDERR) throw(ex); W20160515-13:38:19.761(-5)? (STDERR) ^ W20160515-13:38:19.926(-5)? (STDERR) Error: After filtering out keys not in the schema, your modifier is now empty W20160515-13:38:19.927(-5)? (STDERR) at [object Object].doValidate (packages/aldeed_collection2-core/lib/collection2.js:374:1) W20160515-13:38:19.927(-5)? (STDERR) at [object Object].Mongo.Collection.(anonymous function) [as update] (packages/aldeed_collection2-core/lib/collection2.js:173:1) W20160515-13:38:19.927(-5)? (STDERR) at Function.<anonymous> (server/modules/generate-accounts.js:65:14) W20160515-13:38:19.927(-5)? (STDERR) at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1022:16) W20160515-13:38:19.927(-5)? (STDERR) at Cursor.(anonymous function) [as forEach] (packages/mongo/mongo_driver.js:869:44) W20160515-13:38:19.927(-5)? (STDERR) at Object.generateAccountsBFBL (server/modules/generate-accounts.js:25:16) W20160515-13:38:19.928(-5)? (STDERR) at _generateAccountsBFBL (server/modules/startup.js:20:50) W20160515-13:38:19.928(-5)? (STDERR) at Object.startup (server/modules/startup.js:1:21) W20160515-13:38:19.928(-5)? (STDERR) at server/startup.js:1:38 W20160515-13:38:19.928(-5)? (STDERR) at /Users/adriangracia/com223/ilstuwards_admin_portal/.meteor/local/build/programs/server/boot.js:290:5

I feel like I have tried everything but nothing seems to work.

Alright I have a solution but its not that great. I copied everything over into a new collection through meteor so that I can get rid of the ObjectID’s and afterwards dropped the old collection. I don’t recommend mongoimport with meteor. If anyone runs into this issue look at papaparser. Thanks.

1 Like

I had a similar problem: I was performing a meteor.call with an update to an array and needed to assign an item in that array with a Monogo new object ID.

I was able do it successfully with the following:

const newObjectId = new Mongo.ObjectID().toString();
2 Likes

Just going to leave this note here in case anyone makes the same mistake I made. It should’ve been obvious but inserting the toString() version of ObjectID would result in Mongo escaping quotes, and that’s not the correct way to actually insert a document with an ObjectID.

const _id = new Mongo.ObjectID()
console.log(_id)
console.log(_id.toString())

Will output:

ObjectID { _str: '8bd3f19609a0491aa63d8388' }
ObjectID("8bd3f19609a0491aa63d8388")

When you do a Collection.insert({ _id, ..someOtherFields }), you want:

const _id = new Mongo.ObjectID()

And NOT:

const _id = new Mongo.ObjectID().toString()

The first will correctly insert a document into MongoDB with:

"_id" : ObjectId("8bd3f19609a0491aa63d8388")

The second will incorrectly insert a document into MongoDB with:

"_id" : "ObjectID(\"8bd3f19609a0491aa63d8388\")"
1 Like