我已经读过这个问题并且不明白.是否有能力通过C#驱动程序执行任意mongodb shell脚本?
var mongoServer = MongoServer.Create("mongodb://"); var database = mongoServer.GetDatabase("mydatabase"); string mycollectionCount database.Eval("function() { return db.mycollection.count(); }").ToString();
当您尝试更改属性类型时,这非常有用,例如:
string updateScript = @" function () { db.some_items.find().forEach(function(documentItem) { documentItem.some_collection.forEach(function(collectionItem) { if (typeof collectionItem.SomeProperty === 'number' && Math.floor(collectionItem.someProperty) === collectionItem.someProperty) { collectionItem.someProperty = '' + collectionItem.someProperty; } }); db.modules_elementary.save(documentItem); }); return true; }"; var updateResult = MongoReadDatabase.Database.Eval(updateScript).ToString(); if (updateResult != "true") { throw new ApplicationException("Update of something failed"); }
此代码更改的类型someProperty
是集合集合的元素:
some_items mongo collection: { some_collection: [{ someProperty: 12, ....}], .... }