Skip to content

Instantly share code, notes, and snippets.

@wavded
Created November 8, 2011 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavded/1348429 to your computer and use it in GitHub Desktop.
Save wavded/1348429 to your computer and use it in GitHub Desktop.
Populate a users contacts - works in 2.3.9 but not in 2.3.10
#!/usr/bin/env coffee
global.mongoose = require 'mongoose'
mongoose.connect 'mongodb://localhost/test'
Schema = mongoose.Schema
User = new Schema
userId : { type: String, index: true, required: true }
contacts : [ { type: Schema.ObjectId, ref: 'User' } ]
User = mongoose.model('User', User)
User.remove {}, (err) -> console.log(err) if err
console.log "- cleaning out existing users"
contacts = []
userIds = [ 'A', 'B', 'C', 'D' ]
numRoles = userIds.length
userIds.forEach (userId,index) ->
User.create { userId: userId }, (err, user) ->
if err then console.log err
console.log "+ creating user: #{user.userId}"
contacts.push(user) unless user.userId is 'A'
if --numRoles <= 0
console.log "+ about to update contacts", contacts
User.update {userId: 'A'}, { contacts: contacts }, (err) -> # NEVER GETS PAST HERE IN 2.3.10
if err then console.log err
console.log "+ updating testarc user to include contacts"
process.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment