0

How can I determine what collection a Mongo document belongs to?

I'm using MeteorJS, but even if you don't use this, I'm pretty sure I have access to MongoDB's internal commands so please answer anyways.

Happy to close if this is a duplicate, but I didn't find anything close to this.

JohnAllen
  • 7,317
  • 9
  • 41
  • 65
  • Perhaps I didn't get what you want but, to get the document, you need the collection name first to query the database. Can you share and example on how this would be used? – jpgrassi Dec 24 '15 at 19:57
  • http://stackoverflow.com/questions/9728196/get-a-document-in-mongodb-without-specifying-collection you're much better off tracking the collection name someplace at the time of query – pvg Dec 24 '15 at 19:57
  • http://stackoverflow.com/questions/22309611/find-the-collection-name-from-document-id-in-meteor-mongodb : probably one method is to lookup the keys of all collections and match it with the given `_id` at the cost of performance. – rhitz Dec 24 '15 at 20:00
  • I pass a Mongo doc to a function. This document can be from two collections. I want to get it without writing extra code to find out which kind it is. – JohnAllen Dec 25 '15 at 01:18
  • I can easily write code to figure out, was just hoping for a more succinct way and was curious to learn... – JohnAllen Dec 25 '15 at 01:18

1 Answers1

1

There isn't a real short command but to cycle through each collection and query for the field, or fields that uniquely identifies this document. Don't know MeteorJS but if you are looking for just a quick way to get the data to find out and not add a program function you can download RoboMongo. Think of it as SQL Management Studio for MongoDB. If you are looking for a program function to do this then I suggest you make it a jscript function you create inside MongoDB (similar to stored procedures) to do the work and return the results to you when done.

Are you querying the document now and wondering where it is coming from? If so, it should be in the code.

Parrish
  • 159
  • 5
  • I already have the doc and want to update its collection. The doc can be from one of two collections. To satisfy curiosity, because maybe this isn't a common use case, it's either a price or SEC filing for a quantitative finance app that is a data point. – JohnAllen Dec 25 '15 at 01:21
  • Sorry, been on a long project. If you have the document inside is an object id. You can use that to query each collection to see which one brings up the matching document. Yeah, not really a known scenario to dynamically know what collection a document is from. If you are trying to put this in as a solution I would maybe add another field for the collection it is from in the documents. Maybe make it more cryptic in naming that your code knows and keep it hidden in all your views to be used for getting back to its collection maybe? – Parrish Jan 25 '16 at 00:37
  • Good thought to add it to the collection. Here it was for convenience and was not required - was just a nice to have. – JohnAllen Jan 25 '16 at 02:05