C simple operation example of adding deleting changing and checking MongoDB

  • 2020-05-17 06:18:16
  • OfStack

The C# driver supported by MongoDB is used, and the current version is 1.6.0

Download address: https: / / github com mongodb/mongo - csharp - driver/downloads

1. Connect to the database


        /// <summary>
        ///  Database connection 
         /// </summary>
        private const string conn = "mongodb://127.0.0.1:27017";
        /// <summary>
        ///  Specified database 
         /// </summary>
        private const string dbName = "mongodb_name";
        /// <summary>
        ///  The specified table 
         /// </summary>
        private const string tbName = "table_text";
     // Create a data connection 
         MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
         MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
         MongoCollection col = db.GetCollection(tbName);

2. Insert data

Since MongoDB has no concept of a table, it defines its own data model before inserting the data

User.cs

Here is the code to add the data


        /// <summary>
        ///  add 
        /// </summary>
        /// <param name="text"> content </param>
        /// <param name="articleId"> The article ID</param>
        /// <param name="channelId"> channel ID</param>
        /// <returns></returns>
        public static void Add(User t)
        {
            // Create a data connection 
            MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
            MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
            MongoCollection col = db.GetCollection(tbName);
            // insert 
            col.Insert(t);
        }

3. Delete operation


        /// <summary>
        ///  According to the ObjectID  delete 
        /// </summary>
        /// <param name="objId"></param>
        public static void Delete(string objId)
        {
            // Create a data connection 
            MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
            MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            IMongoQuery query = Query.EQ("_id", new ObjectId(objId));
            col.Remove(query);
        }

4, modify,

        /// <summary>
        ///  According to the ObjectID  Modify the 
        /// </summary>
        public static void Update(User t)
        {
            // Create a data connection 
            MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
            MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            BsonDocument bd = BsonExtensionMethods.ToBsonDocument(t);
            IMongoQuery query = Query.EQ("_id", t.Id);
            col.Update(query, new UpdateDocument(bd));
        }

5. Conditional query (simple)


        /// <summary>
        ///  According to the ObjectID  The query 
        /// </summary>
        public static TuCao SelectOne(string objId)
        {
            // Create a data connection 
            MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
            MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            // Conditions of the query             
            return col.FindOne(Query.EQ("_id", new ObjectId(objId)));
        }

6, query all


        /// <summary>
        ///  Query all 
        /// </summary>
        public static void SelectAll()
        {
            List<User> list = new List<User>();
            // Create a data connection 
            MongoServer server = MongoServer.Create(conn);
            // Gets the specified database 
            MongoDatabase db = server.GetDatabase(dbName);
            // Access to table 
            MongoCollection<User> col = db.GetCollection<User>(tbName);
            // All the query 
            list.AddRange(col.FindAll());
            //--------------------------------
            foreach (TuCao t in col.FindAll())
            {
                string id = t.Id.ToString();
                string text = t.Text;
                DateTime dt = t.Createdate;
            }
        }


Related articles: