MongoDB cross database query
Asked Answered
S

1

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?

Stomatitis answered 7/11, 2012 at 21:46 Comment(1)
What's the context here? In the shell or using a particular driver?Seel
S
48

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()
Sestos answered 8/11, 2012 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.