Created
November 9, 2011 16:37
-
-
Save wavded/1352000 to your computer and use it in GitHub Desktop.
Pushing a contact (tested against mongoose 2.3.11)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
userIds = [ 'A', 'B', 'C', 'D' ] | |
userA = null | |
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}" | |
userA = user if user.userId is 'A' | |
if --numRoles <= 0 | |
console.log(userA) | |
User.update {userId: { $ne: 'A'}}, { $push: { contacts: userA } }, -> | |
if err then console.log err | |
User.find {}, (err, users) -> | |
if err then console.log err | |
console.log(users) # users B,C,D should have user A in their contacts | |
process.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment