How do I call Mongo ObjectID's from Meteor?


#1

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.


#2

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


#3

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.


#4

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?


#5

It should be

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

#6

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.


#7

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.


#8

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();