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

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.

Related

How to set the ReadPreference in mongodb with java api

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();

How can I run flapdoodle MongodStarter with --auth option

I recently configured my mongoDB to use --auth while starting the process.
Now I need to change the accessing Javacode and the tests. For the tests I am using flapdoodle tools like MongodStarter.
There are some example codes at http://www.programcreek.com/java-api-examples/index.php?api=de.flapdoodle.embed.process.config.IRuntimeConfig
But I still need help to build the correct code to start the MongodStarter with the --auth option.
For example I could do this
MongodStarter runtime = MongodStarter.getDefaultInstance();
IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder.MongoCmdOptions(null, null, true, false, false, false, false, true, true)
def mongodConfig = new MongodConfigBuilder.ImmutableMongodConfig(Version.V3_0_4, new Net(27017, Network.localhostIsIPv6()),new Timeout(), cmdOptions, "CT.pid", new Storage(), false, null, new HashMap())
mongodExe = runtime.prepare(mongodConfig);
mongod = mongodExe.start();
mongo = new Mongo("localhost", 27017);
But in the .MongoCmdOptions()-method there are a lot of parameters, which I don't care about and don't know suitable values. Only the 8th is the one, that I want to use. It is the --auth param. Filling the others with some values, I get exceptions.
A similar problem I have with MongodConfigBuilder.ImmutableMongodConfig(). I put some fantasy values to it, because there are a lot.
Can you give me a working example configuration I can test with? I crafted for some days now, but still didn't find a good combination of configuration types and values. I could imagine, that there is a simpler way, but I could not find an example.
This "embedded-services" library has the authentication configuration built in for flapdoodle's Embedded MongoDB. The full working example you want is in MongoEmbeddedService.java file (line 179)
It's not a bad idea to use this tool instead of writing your own. Otherwise, make sure you create yourself an admin user.

What's the easiest way to store a large object in Meteor using GridFS?

I'm trying to backup a Lunr Index to Mongo (daily), and because it's running around 13MB, I'm triggering MongoError: document is larger than capped size errors. I'd like to use GridFS to get around the problem, but I'm having a heck of a time getting it to click.
In the simplest terms: Within Meteor, I'd like to save a 13MB JSON object to MongoDB using GridFS, and then be able to retrieve it when necessary -- all only on the server.
I've gone through the File-Collection and CollectionFS docs, and they seem far too complicated for what I'm trying to accomplish, and don't seem to address simply storing the contents of a variable. (Or, more likely, they do, and I'm just missing it.)
Here's what I'd like to do, in pseudo-code:
Backup = new GridFSCollection('backup');
var backupdata = (serialized search index data object);
Backup.insert({name:'searchindex', data:backupdata, date:new Date().getTime()});
var retrieved = Backup.findOne({name:'searchindex'});
Any suggestions?
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
var GridStore = MongoInternals.NpmModule.GridStore;
var largedata = new GridStore(db,'name','w'); //to write
largedata.open(function(error,gs){
if (error) return;
gs.write('somebigdata',function(error,done){
if (error) return;
largedata.close();
})
});
var largedata = new GridStore(db,'name','r'); //to read data
largedata.open(function(error,gs){
if (error) return;
gs.read(function(error,result){
if (error) return;
//then do something with the result
largedata.close();
})
});
explanation
First you need the db object, which can be exposed via the MongoInternals https://github.com/meteor/meteor/tree/devel/packages/mongo
Second, you need the GridStore object, you get it from the MongoInternals as well
Then you can create a write or read object and follow the API http://mongodb.github.io/node-mongodb-native/1.4/markdown-docs/gridfs.html
The mongo used by Meteor is 1.3.x whereas the latest node mongodb native driver is 2.x.x http://mongodb.github.io/node-mongodb-native/2.0/api-docs/
So the API may change in the future. Currently, you need to do open and close and the callback are all async, you may want to wrap them with Meteor.wrapAsync (may not work on largedata.open), Future or Fiber

Getting a connection to MongoDB database from URL with db name in it

I have a MongoDB connection string with the DB included. I am currently initializing the collection this way:
var mongoUrl = new MongoUrl(_connectionString);
var mongoClient = new MongoClient(mongoUrl);
var mongoServer = mongoClient.GetServer();
var mongoDatabase = mongoServer.GetDatabase(mongoUrl.DatabaseName);
_defaultCollection = mongoDatabase.GetCollection<Comment>("comments");
This works fine but just seems longer than it should be. Am I missing a less chatty way of doing the same thing?
Thanks
Matthew
Unfortunately you're not missing out on a thing - it's indeed a cumbersome way of achieving a common task.
I would encourage you to have a look at the excellent open source MongoRepository project which makes such simple tasks a breeze.

Phonegap and Nova Data Framework -

I am learning PhoneGap for an app project and need to use the database for certain aspects, I am trying out the Nova Data framework,
https://cordova.codeplex.com/wikipage?title=How%20to%20use%20nova.data
I am trying to use my code to put together a test entity, but I am getting a db error telling me there is a missing table. The documentation does not specify that the database should be created beforehand, but I am starting to think that may be the case. Has anyone out there used the Nova framework in a project? I just need a little guidance.
Here is my code I am using to kick off the DB Context:
var DataContext = function () {
nova.data.DbContext.call(this, "HealthDb", "1.0", "Health DB", 1000000);
this.Temperatures = new nova.data.Repository(this, Temperature, "Temperatures");
};
DataContext.prototype = new nova.data.DbContext();
DataContext.constructor = DataContext;
And my entity (Temperature) :
var Temperature = function () {
nova.data.Entity.call(this);
this.Value = 101;
};
Temperature.prototype = new nova.data.Entity();
Temperature.constructor = Temperature;
It is creating an empty database with the proper name, just no tables! I am grateful for any assistance!
Thanks for using our library. I have made the html5 sqlite as a standalone library. Please get it from github.
A live demo link is also available there. And the documentation is more complete. The lib itself has also been updated and a few bugs fixed.
Thanks,
Leo
Turns out I was trying to start up the dbcontext before I defined my entity classes....
Changed the order of my js files and it works.