[原创]C#的mongodb.Driver的In查询、联合查询实例代码

1、In查询

        int[] inselllistid = new int[] { 169199, 169200 };
         MongoHelper mong = new MongoHelper(mongo, "mulan");
        var builder = Builders<SellOrderList>.Filter;
        //var filter = builder.In(n => n.Partner_ID, new int[] { 1, 2, 6 }) & builder.Eq("Seller_ID", 88);
        var filter = builder.In(n=>n.Identifier, inselllistid);
        mong.DeleteMany<SellOrderList>("SellOrderList", filter);

2、联合查询

        //多条件查询
        var builder = Builders<ClientInfo>.Filter;
        var filter = builder.In(n => n.Partner_ID, new int[] { 1, 2, 6 }) & builder.Eq("Seller_ID", 88);
        filter = filter | builder.Eq("Client_Class", "公共") | builder.Eq("Client_Class", "本地");
        List<ClientInfo> cls = mong.GetDocumentsByFilter<ClientInfo>("ClientInfo", filter);
        List<ClientInfo> ls = query.Select().ToList();
        var mongodb = new MongoClient(settings);
        mong.DropCollection<ClientInfo>("ClientInfo");

        var builder = Builders<ClientInfo>.Filter;
        var filter1 = builder.Or(builder.Eq("Seller_ID", 96), builder.Eq("Client_Class", "公共"));
        var filter2 = builder.Or(builder.Eq("Client_Class", "本地"), builder.In(n => n.Partner_ID, new[] { 1, 2, 6 }));
        var filter = builder.Or(filter1, filter2);
        //排序
        SortDefinitionBuilder<ClientInfo> builderSort = Builders<ClientInfo>.Sort;
        SortDefinition<ClientInfo> sort = builderSort.Descending("Create_Date");

        List<ClientInfo> cls = business.SelectByPage<ClientInfo>(ref p, filter, sort);
        Console.WriteLine("j:" + JsonConvert.SerializeObject(query.filter));
        // Here's how you can debug the generated query
        //调试输出mognodb的查询语句
        var documentSerializer = MongoDB.Bson.Serialization.BsonSerializer.SerializerRegistry.GetSerializer<ClientInfo>();
        var renderedFilter = query.filter.Render(documentSerializer, MongoDB.Bson.Serialization.BsonSerializer.SerializerRegistry).ToString();
        Console.WriteLine(renderedFilter);

        List<ClientInfo> pls = mong.GetDocumentsByFilter<ClientInfo>("ClientInfo", query.filter);
赞(1) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏