Ejemplo n.º 1
0
        /// <summary>
        /// 取得全部會員
        /// </summary>
        /// <returns></returns>
        public GetMemberListResponse Get()
        {
            var response = new GetMemberListResponse();

            MongoClient       client = new MongoClient("mongodb://localhost:27017");
            MongoDatabaseBase db     = (MongoDatabaseBase)client.GetDatabase("ntut");
            var membersCollection    = db.GetCollection <MembersCollection>("members");

            //空的查詢式
            var query = new BsonDocument();
            //查詢並取得結果
            var cursor = membersCollection.Find(query).ToListAsync().Result;

            foreach (var doc in cursor)
            {
                response.list.Add(
                    new MemberInfo()
                {
                    uid = doc.uid, name = doc.name, phone = doc.phone
                }
                    );
            }

            return(response);
        }
Ejemplo n.º 2
0
        public GetMemberListResponse Get()
        {
            /* 宣告指令的輸出結果 */
            var response = new GetMemberListResponse();

            /* Step1 連接MongoDB伺服器 */
            MongoClient client = new MongoClient("mongodb://localhost:27017");

            /* Step2 取得MongoDB資料庫(Database)和集合(Collection) */
            /*   Step2-1 取得ntut資料庫(Database) */
            MongoDatabaseBase db = client.GetDatabase("ntut") as MongoDatabaseBase;
            /*   Step2-2 取得members集合(Collection) */
            var membersCollection = db.GetCollection <MembersCollection>("members");

            /* Step3 取得全部會員的資訊 */
            /*   Step3-1 設定空的查詢式,即查詢全部的資料 */
            var query = new BsonDocument();
            /*   Step3-2 進行查詢的操作,並取得結果集合 */
            var cursor = membersCollection.Find(query).ToListAsync().Result;

            /* Step4設定指令的輸出結果 */
            foreach (var doc in cursor)
            {
                response.list.Add(
                    new MemberInfo()
                {
                    uid = doc.uid, name = doc.name, phone = doc.phone
                }
                    );
            }

            return(response);
        }