Mongodb C# how do I set newCollectionsUsePowerOf2Sizes=false; - mongodb

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.

Related

Create mongo change stream in the mongo shell

MongoDB introduced change streams in their 3.6 release.
I wanted to implement mongo change stream in my code and wanted to understand how it works. I will implement using the java driver and it's pretty clear.
But I wanted to know if there is there any way to open a change stream on in the mongo shell? Couldn't find much resources on that.
The db.collection.watch command opens a change stream cursor.
For example:
watchCursor = db.getSiblingDB("data").sensors.watch(
[
{ $match : {"operationType" : "insert" } }
]
)
while (!watchCursor.isExhausted()){
if (watchCursor.hasNext()){
print(tojson(watchCursor.next()));
}
}
Plenty more detail in the docs.

MongoDb toArray

I am using mongodb and in the following code I am using mongodb find().toArray(), but it is giving me the error "Cannot read property 'toArray' of undefined"
req.activedb.collection('items').find().toArray(function (err, data) {
//...some code
})
whereas when I am using findOne(), then it is working properly.
req.activedb.collection('items').findOne(function (err, records) {
console.log(err, records); //Getting a single record here
})
req.activedb is my current db instance
Can you please tell me, what is missing here ?
I have resolved this issue.
Actually I was using mongoose to connect with my db, so it does not support find(). So now I am connecting to the db using new Db() method and it is working properly.
I have run belows command its work for me.
posts is nothing but collection.
db.posts.find().toArray();
You might be, you are making mistake in syntax .

how to implelement custom mongodb driver

I using mongodb in my poject and interesting how to implement simple mongodb driver which will allow to run mongodb command like in a shell form. Just writing command in javascript and pushing it to mongodb, then geting result and deserializing it.
Substitute your connection string in the code below.
While it would be extremely dangerous to provide this generally speaking (as it allows any command to execute), just call the eval function on the instance of the database that's returned.
This code is use the JavaScript/NodeJS driver:
var
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient
;
MongoClient.connect("mongodb://localhost:27017/default?w=1",
function (err, db) {
db.eval("db.version()", null, {noLock: true}, function(err, results) {
console.log(results);
});
});

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.

Partial Update on MongoDB Error

I'm trying to have a partial update on one of my documents in MongoDB, using C# driver. I've followed the following posts:
How do you update multiple field using Update.Set in MongoDB using official c# driver?
Partial mongodb upsert using the c# driver?
I get the following error when trying to do the update: "Only classes can be mapped currently", in the AutoMapper CreateClassMap class, the type received is System.Collections.Generic.IEnumerable`1[[MongoDB.Bson.BsonElement, MongoDB.Bson]], where it cannot be an interface.
The code I'm using is:
public void UpdateObjectByFields<T>(int id, Dictionary<string, object> modifiedFields)
where T : class
{
var collection = m_MongoDatabase.GetCollection<T>();
var builder = new UpdateBuilder();
foreach (var modifiedField in modifiedFields)
{
builder.Set(modifiedField.Key, modifiedField.Value.ToString());
}
collection.Update(Query.EQ("_id", id), builder);
}
where the T type is a valid collection in the Mongo.
What am I doing wrong?
Thanks,
Nir
Got this to work, apparently I was using some old dlls for the C# driver.
Fixed it by using the dlls from here:
https://github.com/mongodb/mongo-csharp-driver/downloads
Thanks,
Nir.