MongoDb:使用$ divide和$ gte发行

时间:2018-12-26 10:17:32

标签: c# mongodb filter

我是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.CommandWireProtocol 1.ProcessReply(ConnectionId connectionId, ReplyMessage 1答复)在   MongoDB.Driver.Core.WireProtocol.CommandWireProtocol 1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol 1   协议,在   MongoDB.Driver.Core.Operations.CommandOperationBase 1.ExecuteProtocolAsync(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.ReadCommandOperation 1.ExecuteAsync(IReadBinding   绑定,在   MongoDB.Driver.Core.Operations.FindCommandOperation 1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.FindOperation 1.ExecuteAsync(IReadBinding   绑定,在   MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync [TResult](IReadBinding   绑定,IReadOperation 1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl 1.ExecuteReadOperationAsync [TResult](IClientSessionHandle   会话IReadOperation 1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl 1.UsingImplicitSessionAsync [TResult](Func 2 funcAsync, CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource 1   来源,在   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}

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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();