25

I know I've seen this solution somewhere, but I can't seem to find it now. I'm trying to query one MongoDB database while connected to another. This should be possible without explicitly connecting to the other database before running the query. Does anyone know the correct syntax to do this?

1
  • What's the context here? In the shell or using a particular driver? Commented Nov 7, 2012 at 22:38

1 Answer 1

49

To run a command against another database on the same MongoDB server, in the mongo shell you can use:

db.getSiblingDB('dbname').collection.command()

eg:

db.getSiblingDB('test').foo.find()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.