.Net Core 3.0 对 MongoDB 的多条件查询(两种)操作

在日常开发中,偶尔会用到 MongoDB 的数据操作,也花费了一些时间调试,因此在此处记录一下,共同进步。

废话少说,出招吧!

首先需要引入 .Net 平台链接 MongoDB 的动态库: MongoDB.Driver

然后 创建默认 DBContext 实体类

(将数据库以及表的信息引入到系统中备用,数据库链接信息最好是放置在配置文件中,便于不同环境灵活配置,次非本文重点就直接默认了)

using MongoDB.Bson;
using MongoDB.Driver;

namespace DBContext.DataBase
{
    public class MongoDBContextDefault
    {
        //连接地址
        private static string conn = "mongodb://0.0.0.0:0000";
        //连接服务端
        static MongoClient client_pub = new MongoClient(conn);
        //数据库名称
        private static string dbName = "MongDBName";
        //集合名称
        private static string collName_Table1 = "Table1";
        private static string collName_Table2 = "Table2";
        //获取指定数据库
        static IMongoDatabase db_Test = client_pub.GetDatabase(dbName);
        //获取指定集合   BsonDocument数据库文档对象
        public static IMongoCollection<bsondocument> colle_Table1 = db_Test.GetCollection<bsondocument>(collName_Table1);// BsonDocument&#xFF1A;&#x6700;&#x7EC8;&#x4F1A;&#x4EE5; Json &#x5BF9;&#x8C61;&#x6765;&#x53D6;&#x503C;
        public static IMongoCollection<table2model> colle_Table2Model = db_Test.GetCollection<table2model>(collName_Table2);// Table2Model&#xFF1A;&#x4E0E;&#x8868;&#x5B57;&#x6BB5;&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x53D6;&#x503C;&#x65B9;&#x4FBF;
    }
}</table2model></table2model></bsondocument></bsondocument>

直接上代码,看注释即可:

(传入的查询条件,需要和数据库中的类型匹配)(若通过 ID 查询,需要将条件值转为 Guid 类型)

using DBContext.DataBase;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
public List<jobject> GetValues(string name, string id)
{
    FilterDefinitionBuilder<bsondocument> builderFilter = Builders<bsondocument>.Filter;
    DateTime startTime = DateTime.Now.AddDays(-7);//&#x53EA;&#x540C;&#x6B65;&#x6700;&#x8FD1;&#x4E03;&#x5929;&#x6709;&#x66F4;&#x65B0;&#x7684;&#x6D41;&#x7A0B;
    FilterDefinition<bsondocument> filter = builderFilter.And(builderFilter.Eq("name", name), // &#x591A;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;
        builderFilter.Eq("id", new Guid(id)), // id &#x9700;&#x8981;&#x8F6C;&#x6362;&#x6210; Guid &#x7C7B;&#x578B;
        builderFilter.Gte("stime", startTime)); // Time &#x9700;&#x8981;&#x662F; DateTime &#x7C7B;&#x578B;
    var result = MongoDBContextDefault.colle_ProcInstData.Find<bsondocument>(filter).ToList(); // &#x83B7;&#x53D6;&#x6570;&#x636E;
    List<jobject> jTokens = new List<jobject>();
    var setjson = new JsonWriterSettings
    {
        OutputMode = JsonOutputMode.Strict
    };
    foreach (var item in result) // &#x9010;&#x4E2A;&#x8F6C;&#x4E3A; JObject&#xFF0C;&#x5907;&#x7528;&#xFF08;&#x540E;&#x7EED;&#x5FAA;&#x73AF;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7; JObjectItem["name"] &#x8FDB;&#x884C;&#x53D6;&#x503C;&#xFF09;
    {
        JObject item_jo = JObject.Parse(item.ToJson(setjson));
        jTokens.Add(item_jo);
    }
    return jTokens;
}</jobject></jobject></bsondocument></bsondocument></bsondocument></bsondocument></jobject>

此方案中的查询字符串和上一章节是有区别的,具体如下:

(查询条件先拼接成 json 字符串,再转 BsonDocument 对象)

public void Method(string name,string id)
{
    DateTime dtcondition = DateTime.Now.AddDays(-7);
    // &#x65F6;&#x95F4;&#x4E0A;&#x5DEE;&#x4E00;&#x4E2A;&#x6708;&#xFF0C;&#x6240;&#x4EE5;&#x6708;&#x4EFD; -1 // &#x5177;&#x4F53;&#x539F;&#x56E0;&#x672A;&#x77E5;&#x3002;&#x3002;&#x3002;&#x5F85;&#x540E;&#x7EED;&#x8865;&#x5145;
    string strdate = $"new Date({dtcondition.Year},{dtcondition.Month - 1},{dtcondition.Day})";
    string querystr = $"{{$and:[{{'name':'{name}'}}" +
        $",{{'id':CSUUID('{id}')}}" + // &#x53E6;&#x4E00;&#x79CD;&#x6839;&#x636E; ID &#x67E5;&#x8BE2;&#x65B9;&#x5F0F;
        $",{{'time':{{$gte:{strdate}}}}}]}}";
    var querycon = BsonSerializer.Deserialize<bsondocument>(querystr);
    var dataresult = MongoDBContextDefault.colle_Table2.Find(querycon).ToList();
    // &#x8FD9;&#x91CC;&#x7684; dataresult &#x7ED3;&#x679C;&#x7C7B;&#x578B;&#x662F; List<table2model>,&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8DDF;&#x5C5E;&#x6027;&#x53D6;&#x503C;&#xFF0C;&#x66F4;&#x65B9;&#x4FBF;
}</table2model></bsondocument>

注:暂列这两种查询吧,后续继续补充,有问题欢迎指正。

Original: https://www.cnblogs.com/czzj/p/16858742.html
Author: 橙子家
Title: .Net Core 3.0 对 MongoDB 的多条件查询(两种)操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/799345/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球