How to override Accounts _id?

1 ) have the following code in my server/seeders.js:

var user1 = Accounts.createUser({ "_id": "123456", "username": "user1", "email": "user1@example.com", "password": "hunter2" });

It still automatically generates its own random _id, how can I override it?

  1. If the user was created previously, is it possible to update the user’s _id?

Thanks!

I would say do this immediately after creating the user:

var col = Bookmarks.rawCollection();
Meteor.wrapAsync(col.update.bind(col))({username: "user1"}, {$set: {_id:'123456'}});

but I get this error:

[Error][MongoError: After applying the update to the document {_id: "yQ8Cd2LgBK7Nv6Gzz" , ...}, the (immutable) field '_id' was found to have been altered to _id: 123]

But actually, you may be able to do it in onCreateUser:

http://docs.meteor.com/#/full/accounts_oncreateuser

try setting the id there before returning the user object.

Also, not sure what you wanna update the user id to, but you may wanna add the random package and generate an id with Random.id().

If worse, comes to worse, you can just remove the created user, copy its props, and manually insert a user into the database. The internal createUser process does a lot for you and as a result doesn’t allow you to do too much. But regularly you can insert an object and set the ID. That’s how IDs are set on the client in mini mongo when you insert a doc from the client. The server uses the same ID, which is guaranteed to be like a number so random, it might not even exist anywhere else in the world. Something like that lol. I’ve wondered about collisions there for a while, but basically the chances of a random # being generated that your app already has are so low that it affords Meteor the convenience of giving you an instant ID on the client before any async response that the doc was actually inserted, which can be immediately/synchronously use for other purposes.