通过C#驱动执行mongodb shell脚本

发布于 2024-11-28 21:45:47 字数 113 浏览 0 评论 0原文

我已阅读这个问题但不明白。是否能够通过 C# 驱动程序执行任意 mongodb shell 脚本?

I have read this question and haven't understand. Is there ability to execute arbitrary mongodb shell script via C# driver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-12-05 21:45:48
var mongoServer = MongoServer.Create("mongodb://<connectionstring>"); 
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, ....}],
   ....

}
var mongoServer = MongoServer.Create("mongodb://<connectionstring>"); 
var database = mongoServer.GetDatabase("mydatabase"); 
string mycollectionCount database.Eval("function() { return db.mycollection.count(); }").ToString();

This is useful when you are trying to change property types for example like this:

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");
}

This code changes type of someProperty which is element of a collection of a collection:

some_items mongo collection:

{
   some_collection: [{ someProperty: 12, ....}],
   ....

}
牛↙奶布丁 2024-12-05 21:45:48

不,您需要启动 Mongo shell 进程,使用类似 Process.Start,并传入要执行的命令,例如

mongo.exe mydb --eval "printjson(db.getCollectionNames())"

C# 驱动程序 可以完成 shell 可以做的大部分事情,因此如果可能的话,直接使用驱动程序会更容易。

No, you'd need launch a Mongo shell process, using something like Process.Start, and pass in the command you want to execute, e.g.

mongo.exe mydb --eval "printjson(db.getCollectionNames())"

However, the C# driver can do most things the shell can, so if possible it's much easier to use the driver directly.

薄荷梦 2024-12-05 21:45:48

我还没有尝试过,但我认为这就是您正在寻找的:

MongoServer.RunAdminCommand 方法(字符串)
http://api.mongodb.org/csharp/1.1/html/a83249ae-0989-7c24-7240-4506053d83c1.htm

I have not tried it but I think this is what you are looking for:

MongoServer.RunAdminCommand Method (String)
http://api.mongodb.org/csharp/1.1/html/a83249ae-0989-7c24-7240-4506053d83c1.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文