简体   繁体   中英

How to fetch data from two collections in one query in mongo db?

Let's say I have two collections A and B. In my C# program I'm trying to get data from them. Currently I get data from collection A first and save the documents into a container. And then get data from collection B. But theoretically speaking, the data may be changed after getting from A and before getting from B. So that I get incorrect results. Is there a safe way?

Mongodb does not support transactions. Because of mongodb was designed to be scalable (it means sharding, replication), and distributed transactions can be very expensive and make database slower in general. So you can't block collection A for writes, while you getting/updating it, loading from B.

There is some kind of transactions in mongodb -- it is atomic updates on single document. So you can update part of document without loading of it.

Whilst Mongo doesn't explicitly support locking tables like SQL systems do, you might want to take a look at the fsync function. If I'm reading the docs correctly, you should be able to use it as a hack to temporarily prevent data writes.

http://www.mongodb.org/display/DOCS/fsync+Command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM