{"action":"opened","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3614","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3614/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3614/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3614/events","html_url":"https://github.com/Automattic/mongoose/issues/3614","id":118817352,"number":3614,"title":"Switch collection at runtime","user":{"login":"devvit","id":1715224,"avatar_url":"https://avatars.githubusercontent.com/u/1715224?v=3","gravatar_id":"","url":"https://api.github.com/users/devvit","html_url":"https://github.com/devvit","followers_url":"https://api.github.com/users/devvit/followers","following_url":"https://api.github.com/users/devvit/following{/other_user}","gists_url":"https://api.github.com/users/devvit/gists{/gist_id}","starred_url":"https://api.github.com/users/devvit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devvit/subscriptions","organizations_url":"https://api.github.com/users/devvit/orgs","repos_url":"https://api.github.com/users/devvit/repos","events_url":"https://api.github.com/users/devvit/events{/privacy}","received_events_url":"https://api.github.com/users/devvit/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-25T11:32:36Z","updated_at":"2015-11-25T11:32:36Z","closed_at":null,"body":"Hi, guys, I wanna implement dynamic collection, consider schemas like these:\r\n```coffeescript\r\nmongoose = require 'mongoose'\r\nmongoose.connect 'mongodb://localhost/test_dev'\r\ndb = mongoose.connection\r\n\r\nuserSchema = new mongoose.Schema {\r\n  name: String\r\n  location: String # tokyo, osaka ...\r\n}\r\n\r\npostSchema = new mongoose.Schema {\r\n  title: String\r\n  user: { type: String, ref: 'User' }\r\n}\r\n\r\npostSchema.pre 'save', (next) ->\r\n  # switch collection here\r\n  # just for instance level, not class level\r\n  this.collection = db.collection 'posts_' + this.user.location\r\n  next()\r\n\r\nUser = mongoose.model 'User', userSchema\r\nPost = mongoose.model 'Post', postSchema\r\n\r\ntom = new User { name: 'tom' }\r\ntom.location = 'osaka'\r\ntom.save()\r\n\r\npost1 = new Post { title: 'hello' }\r\npost1.user = tom\r\npost1.save (err, doc) ->\r\n  if err\r\n    console.log err\r\n  else\r\n    console.log doc\r\n    console.log Post.collection.name # Post collection is still 'posts'\r\n```\r\nIt's working fine, but I could not determine whether it is **safe enough**, anybody tell me that ? Thanks\r\n\r\nPS: I write ruby, just a javascript newbie"}}