How to set the ReadPreference in mongodb with java api - mongodb

the Mongoclient.setReadPreference(ReadPreference.primary()) in java api is deprecated
the document shows
Set the default read preference with either MongoClientURI or
MongoClientOptions
but I find MongoClientOptions doesn't have setReadPreference method .but in MongoOptions .but it looks strange ,MongoClientOptions doesn't extends MongoOptions .
firstly I wanted to know what is the relationship about the two options ,did they have the same effection ?
secondly , tell me how to set ReadPreference in java api , you'd better show me your code (MongoClientURI or MongoClientOptions).thanks in advance .

MongoOptions is deprecated. You can't set the readPreference directly. You have to use Builder to set the readPreference.
Something like
MongoClientOptions clientOptions = MongoClientOptions.builder().readPreference(ReadPreference.PRIMARY).build();

Related

How to filter documents in MongoDB and Spring boot

I am creating a Spring boot where I have "articulos" documents. I want to retrieve them from the DB depending on their fields value but I can not seem to achieve it with the examples I found in this website or anywhere else. This is because I see someone people use mongoTemplate which I assume is the interface class I have created for my repository but when I try to use that it says the method wasn't found.
This is what I am trying to do:
Query query = new Query();
query.addCriteria(Criteria.where("name").ne("Eric"));
List<Articulo> articulos = this.articuloRepository.find(query, Articulo.class);
articulosRepository is just and empty interface with all the annotations needed
Create the below repository method in ArticulosRepository
List<Articulo> findByNameNot(String name);
Use like
List<Articulo> articulos = this.articuloRepository.findByNameNot("Eric");
In your repository interface create a named query with your property like below if you have name in your document then method will be:
List<Articulo> articulos = findByName(String name);
Call this from your service by #Autowired repository.

Elasticsearch scala elastic4s settings from property file

is there a way how to pass settings to elastic4s from property file? The following way works but it is not flexible in munltienvironment:
val settings = ImmutableSettings.settingsBuilder().put("cluster.name","elasticsearch").build()
val client = ElasticClient.remote(settings, "154.86.209.242" -> 9300, "153.89.219.241" -> 9300)
I tried java configuration file elasticsearch.yaml as mantioned in java doc but that doesn't work.
Any suggestion here?
You can do this using the same method you would for the Java client. The ImmutableSettings is a Java Client class not something that is specific to elastic4s.
To load your properties file from the classpath, eg if you have something in src/main/resources/com/package/settings.props
ImmutableSettings.settingsBuilder().loadFromClasspath("/com/package/mysettings.yaml")
Or if you want to load from an input stream:
ImmutableSettings.settingsBuilder().loadFromStream(myinputstream)
There are other methods too, just check out the ImmutableSettings.settingsBuilder object.

Mongodb C# how do I set newCollectionsUsePowerOf2Sizes=false;

I am creating a large number of documents and collections and would like to set newCollectionsUsePowerOf2Sizes=false
What is the C# code to set this property.
thanks
I haven't tried this with C# but in mongoshell we can do this:-
db.runCommand( {collMod: "products", usePowerOf2Sizes : true })
This same command can be send using java driver using this function in DB api (link):-
public CommandResult command(DBObject cmd)
You can find a C# equivalent of this. Hope this helps.

Apply default mapping to mock Mongo domains in Grails unit test

I've started using the new mocking support in grails-datastore-gorm-mongodb. My app defaults domain mappings to use references when persisting relationships to mongodb. I need to find a way to get the mocked mongo to do the same thing. How do I apply the same default mapping in a unit test?
In Config.groovy, it looks like this:
// configure mongo to use dbrefs:
grails.mongo.default.mapping = {
'*'(reference: true)
}
Here's a sample of code that I currently use:
import spock.lang.*
import grails.test.mixin.mongodb.MongoDbTestMixin
import com.github.fakemongo.Fongo
#Mixin([MongoDbTestMixin])
class MySpec extends Specification {
def setup() {
mongoDomain(new Fongo("test").mongo, [ MyDomain ])
new MyDomain(name: 'domain').save(validate: false, flush: true)
}
}
How do I apply that config to this test code?
I'm using Grails 2.3.9 and mongodb 3.0.1 plugin.
Looks like MongoDbTestMixin offers a few flavors of the mongoDomain method:
mongoDomain(Mongo mongo, Collection<Class> persistentClasses) - Sets up a GORM for MongoDB domain for the given Mongo instance and domain classes
mongoDomain(Map config, Collection<Class> persistentClasses) - Sets up a GORM for MongoDB domain for the given configuration and domain classes
The 2nd option allows to pass a configuration map which allows to configure mongo to use dbrefs (otherwise an empty configuration is used, see MongoDbDataStoreSpringInitializer ). However this method does not allow you to pass the Fongo instance.
You can try to:
Ask the Grails team to add a method which combines both options (pull request?)
Extend MongoDbTestMixin or create your own mixin

MongoDB : How to find if slaveOk=true is set or not

We are using Mongo DB in our Application .
We have one primary and one secondary for this purpose .
How can i make sure that my Application is reading data from Primary Or Secondary ??
My question is how can i know if slaveOk=true is set or not ??
Thanks in advance .
Edited Part
Not sure of the Driver what i am uisng
I am connecting Mongo DB through Java as shown
ServerAddress addr = new ServerAddress(new InetSocketAddress(host, port));
servAddrList.add(addr);
}
MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.connectionsPerHost = Config.intParam(
"mongo.connectionsPerHost", 1200);
mongo = new Mongo("10.11.13.111", 27017);
And i am using mongo 2.4.jar
Please let m know how can i find what driver i am using ??
What are you looking for in my opinion is readPreference : http://docs.mongodb.org/manual/applications/replication/#replica-set-read-preference
In the api (http://api.mongodb.org/java/2.10.1/com/mongodb/ReadPreference.html) documentation there is an isSlaveOk() method : http://api.mongodb.org/java/2.10.1/com/mongodb/ReadPreference.html#isSlaveOk()
See this question: How to perform read operations from primary only
There is the answer for setting read preference
mongo.setReadPreference(ReadPreference.primary());
and your answer is to get read preference:
mongo.getReadPreference().isSlaveOk();
Unfortunately these features do not exist in versions after 2.4.
looks like earlier java driver just have mongo.slaveOk() method, call it and it is slaveOk. no way to examine.