【问题标题】:MongoDb : Issue using $divide and $gteMongoDb:使用 $divide 和 $gte 的问题
【发布时间】:2019-05-24 15:10:00
【问题描述】:

我是 MongoDb 的新手。我有问题当我在下面的代码中使用$divide$gte 时不要给我错误。但它的过滤器不起作用。

代码:

var filter1 = new BsonDocument()
              { 
                {"expr",
                        new BsonDocument(){
                        {
                            "$gte", new BsonArray{
                                new BsonDocument{
                                {
                                    "$divide", new BsonArray{"$nA", "$wT"}
                                },
                            },
                            sPacketMSItem.FromDPC.Value
                        }
                      }
                    }
                   }
                 };

如果有人要求比告诉我更多的信息。

例外:

{MongoDB.Driver.MongoCommandException:命令查找失败:未知 顶级运算符:$expr。在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ProcessReply(ConnectionId connectionId, ReplyMessage1 回复)在 MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol1 协议,CancellationToken 取消令牌)在 MongoDB.Driver.Core.Operations.CommandOperationBase1.ExecuteProtocolAsync(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.ReadCommandOperation1.ExecuteAsync(IReadBinding 绑定,CancellationToken 取消令牌)在 MongoDB.Driver.Core.Operations.FindCommandOperation1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.FindOperation1.ExecuteAsync(IReadBinding 绑定,CancellationToken 取消令牌)在 MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding 绑定,IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperationAsync[TResult](IClientSessionHandle 会话,IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func2 funcAsync, CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource1 来源,CancellationToken 取消令牌)在 Biz.DAL.MongoRepositoriesCustom.SalesCRMRepository.MarketSheet.MarketSheetRepository.SearchBySearchPacketMSItemAsync(SearchPacketMSItem sPacketMSItem) 在 /Users/lalitdevani/Documents/AasthaSalesWebApi/Aastha/_git/Sales.WebApi/Biz.DAL/MongoRepositoriesCustom/SalesCRMRepository/MarketSheet/MarketSheetRepository.cs:line 142}

任何帮助将不胜感激。

【问题讨论】:

    标签: c# mongodb filter


    【解决方案1】:

    $expr 是一个评估查询运算符,所以它应该以美元符号为前缀,试试:

    var filter1 = new BsonDocument()
                { 
                    {"$expr",
                            new BsonDocument(){
                            {
                                "$gte", new BsonArray{
                                    new BsonDocument{
                                    {
                                        "$divide", new BsonArray{"$nA", "$wT"}
                                    },
                                },
                                sPacketMSItem.FromDPC.Value
                            }
                        }
                        }
                    }
                };
    

    编辑:$expr 在 MongoDB 3.6 或更高版本中可用,作为备用您可以使用 $redact,尝试:

    var redact = new BsonDocument()
            {
            {"$redact",
                    new BsonDocument
                    {
                        {
                            "$cond", new BsonDocument(){
                            {
                                "if", new BsonDocument()
                                {
                                    { "$gte", new BsonArray
                                        {
                                            new BsonDocument{
                                            {
                                                "$divide", new BsonArray{"$nA", "$wT"}
                                            },
                                        },
                                        sPacketMSItem.FromDPC.Value
                                        }
                                    }
                                }
                            },
                            {  "then", "$$KEEP"  },
                            {  "else", "$$PRUNE" }
                        }
                        }
                    }
            }
        };
    
    var result = Col.Aggregate()
                    .AppendStage<BsonDocument>(redact).ToList();
    

    【讨论】:

    • $expr是MongoDB 3.6版本引入的,你用的是什么版本?
    • MongoDB.Driver 版本是2.6.1 Nuget 包。
    • 我的意思是 MongoDB 作为数据库,而不是驱动程序(在 Mongo shell 中检查 db.version())
    • MongoDB 版本是 3.4.4
    • @Ironman 解释了为什么它会抛出异常。您可以升级还是需要解决方法?
    猜你喜欢
    • 2023-04-09
    • 2017-09-06
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2021-09-17
    • 1970-01-01
    相关资源
    最近更新 更多