如何在Monger中使用find-maps时设置ReadPreference? Monger文档仅指定monger.query的with-collection用法,如下所示
typedef struct {
enum {
TA,
TB
} _;
union {
struct {
int a;
};
struct {
float b;
};
};
} C;
C c = { ._ = TA, .a = 1 };
答案 0 :(得分:2)
这样做的一种方法是在连接Mongo时设置读取首选项:
(mg/connect
(mg/server-address "127.0.0.1" 27017)
(mg/mongo-options {:read-preference (com.mongodb.ReadPreference/secondaryPreferred)}))
如果您正在考虑在Java API中使用此方法:https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/DBCollection.html#setReadPreference-com.mongodb.ReadPreference-我找不到通过monger使用此方法的方法。但是,您可以使用自己的函数来利用此API:
(defn find-with-read-preference [db coll]
(.find (doto
(.getCollection db (name coll))
(.setReadPreference (ReadPreference/secondaryPreferred)))))
(defn find-maps-with-read-preference
([^DB db ^String coll]
(with-open [result (find-with-read-preference db coll)]
(map (fn [x] (monger.conversion/from-db-object x true)) result))))