简体   繁体   English

如何使用本机 MongoDB.Driver 将 mongo 脚本转换为纯 C# 代码?

[英]How can I convert mongo script to pure C# code by using native MongoDB.Driver?

How can I convert below mongodb shell script to C# by using "MongoDB.Driver" ?如何使用“MongoDB.Driver”将下面的 mongodb shell 脚本转换为 C#? the script below is working perfect in local.下面的脚本在本地运行完美。 There is no issue in script.脚本没有问题。 But if I publish it as Azure func.但是如果我将它发布为 Azure func。 There is permission issue for "eval "operator. “eval”运算符存在权限问题。 So I decided to rewrite above script as Native C# by using MongoDb.Driver.I developed below code, but "eval" didn't work and throwed error while running in Azure function: "Command eval failed: Command is not supported. ".所以我决定使用 MongoDb.Driver 将上面的脚本重写为 Native C#。我开发了下面的代码,但是在 Azure 函数中运行时“eval”不起作用并抛出错误:“命令 eval 失败:不支持命令。”。 I decided convert to pure C# code.How can I do That?我决定转换为纯 C# 代码。我该怎么做?


    Date.prototype.addDays = function(h) {    
   this.setTime(this.getTime() + (h*60*60*1000*24)); 
   return this;   
} 

var beforeDate = (new Date()).addDays(-7);
var totalDeleted = 0;

do
{

    var ids = db.klm
        .find({
            CreatedDate: {$lt: beforeDate},
            xyz: {$eq: null},
            abc: {$eq: null},
            Items: { $size: 0 }
        })
        .limit(100)
        .map(function (doc) { return doc._id; });

    totalDeleted += ids.length;

    //db.klm.remove({"_id": { "$in": ids }});

} while (ids.length > 0);

print("Deleted " + totalDeleted + " rows before " + beforeDate);

the following will delete everything with the matching filter.以下将删除具有匹配过滤器的所有内容。 it does not delete in batches of 100s as your shell code which would be less efficient.它不会成批删除 100 秒作为您的 shell 代码,这会降低效率。 the following only issues a single mongodb query which would take care of deleting all matching records.以下仅发出一个 mongodb 查询,该查询将负责删除所有匹配的记录。

var beforeDate = DateTime.UtcNow.AddDays(-7);

var filter = Builders<klm>.Filter
                          .Where(k =>
                                 k.createdDate < beforeDate &&
                                 k.abc == null &&
                                 k.xyz == null &&
                                 (k.items.Count() == 0 || k.items == null));

var result = collection.DeleteMany(filter);

Console.WriteLine($"Deleted {result.DeletedCount} documents created before {beforeDate.ToShortDateString()}");

update: the following will result in 2 queries for each batch of 100 records.更新:以下将导致每批 100 条记录进行 2 次查询。

var beforeDate = DateTime.UtcNow.AddDays(-7);
var totalDeleted = 0;
var ids = new List<ObjectId>();

do
{
    ids = collection.Find(k =>
                          k.createdDate < beforeDate &&
                          k.abc == null &&
                          k.xyz == null &&
                         (k.items.Count() == 0 || k.items == null))
                    .Limit(100)
                    .Project(k => k.Id)
                    .ToList();

    if (ids.Any())
    {
        collection.DeleteMany(k => ids.Contains(k.Id));
        totalDeleted += ids.Count();
    }               

} while (ids.Any());

Console.WriteLine($"Deleted {totalDeleted} documents created before {beforeDate.ToShortDateString()}");
Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM