El motivo de los errores intermitentes se debe a las preferencias de lectura predeterminadas del controlador, principalmente en lo que respecta a los conjuntos de réplicas. La preferencia de lectura predeterminada es primaria. Para cada uno de los modos mencionados a continuación, PRIMARIO se refiere a la base de datos maestra (siempre la más actualizada) y SECUNDARIO se refiere a los esclavos, que son básicamente las copias del maestro y no siempre están actualizados.
PRIMARY: The default read mode. Read from primary only. Throw an error if
primary is unavailable. Cannot be combined with tags.
La solución para cambiar la preferencia de lectura a una de las siguientes:
PRIMARY PREFERRED: Read from primary if available, otherwise a secondary.
SECONDARY PREFERRED: Read from a secondary if available, otherwise read from the primary.
NEAREST: Read from any member node from the set of nodes which respond the fastest.
Código de ejemplo:
// Use this when doing a read if you don't care if the data is always consistent.
// Change the following with secondaryPreferred() if you have high writes, so
// that you don't interfere with them.
ReadPreference preference = ReadPreference.primaryPreferred();
DBCursor cur = new DBCursor(collection, query, null, preference);
Para obtener más información, consulte la fuente .