0

How would one find the Collection from the Document object in MongoDB? I'm developing a small game in Meteor and I've run into a scenario in which I have two Document objects that are similar (Players and Monsters) and get called often in similar places (both move, initiate combat, can pick things up, etc).

This is all server side code.

I have functions server side that can interchangeably use these Documents and I won't know which Collection to execute updates against.

I was hoping that there was a sort of "save()" method similar to that of Mongoose but there doesn't appear to be.

Is there a way to introspect a record after executing:

var player = Players.findOne({query});

to know that the results from that query are from Players to be inserted back into later in another function?

Kich867
  • 53
  • 5
  • 1
    Why not just add a field called `collectionFrom` on the document? – Larry Battle Jan 28 '15 at 02:16
  • 1
    Are you using meteor or not? You included the tag for meteor but ask about mongoose. Plain MongoDB, No. Not without storing data. But either of those others produce special objects that you can introspect. – Neil Lunn Jan 28 '15 at 02:23
  • By the way, Meteor doesn't have a save method on collection because changes are automatically saved. It's called `Full Stack Reactivity` More info here: http://docs.meteor.com/#/full/sevenprinciples – Larry Battle Jan 28 '15 at 02:28
  • possible repo of [this](http://stackoverflow.com/questions/10984030/get-meteor-collection-by-name) also take a look on [this](https://github.com/shanedonnelly1/mongoinspector) – Ethaan Jan 28 '15 at 04:32
  • Larry Battle, I didn't want to go that route, I may have to. Also, in regards to full stack reactivity--I'm well aware of what it is, but this is all server-side right now and changing a record I've fetched has no impact on the database without updating it--that's the issue, there are places I won't know what the collection is. I'll edit the question to be more clear. @NeilLunn , I am using meteor, I mentioned a method from Mongoose that I wanted. Could you elaborate on Meteor producing a special object that I can introspect? The docs were unhelpful in this endeavor. – Kich867 Jan 28 '15 at 14:08

0 Answers0