Ejemplo n.º 1
0
        public void Post(PostInfo model)
        {
            var mongoDbClient = new MongoClient("mongodb://127.0.0.1:27017");
            var mongoDbServer = mongoDbClient.GetDatabase("nmbp");
            DateTime date = DateTime.Now;
            
            var document = new BsonDocument
            {               
                //{ "post_id",  model.id  },
                { "post_title",  model.Title  },
                { "post_text",  model.Text  },
                { "post_author",  model.Author  },
                { "post_picture",  model.Picture  },
                { "post_date", date.ToString("dd-MM-yyyy") },
                { "post_comments",  new BsonArray()  },
            };

            var collection = mongoDbServer.GetCollection<BsonDocument>("post");
            collection.InsertOneAsync(document);
        }
Ejemplo n.º 2
0
        public async Task<string> Get()
        {
            var mongoDbClient = new MongoClient("mongodb://127.0.0.1:27017");
            var mongoDbServer = mongoDbClient.GetDatabase("nmbp");
            //string post_id = '"' + postID + '"';

            
            var collection = mongoDbServer.GetCollection<BsonDocument>("post");
            List<PostInfo> postInfo = new List<PostInfo>();
            var filter = new BsonDocument();
            var count = 0;
            var k = 0;
            using (var cursor = await collection.Find(filter).Sort("post").Ascending("post_date").Limit(10))
            {
                
                while (await cursor.MoveNextAsync())
                {
                    var batch = cursor.Current;
                    foreach (var item in batch)
                    {
                        PostInfo post = new PostInfo();
                        post._id = item.GetElement("_id").Value.ToString();
                        post.Title = item.GetElement("post_title").Value.ToString();
                        post.Author = item.GetElement("post_author").Value.ToString();
                        post.Text = item.GetElement("post_text").Value.ToString();
                        post.Picture = item.GetElement("post_picture").Value.ToString();
                        post.Comments = item["post_comments"].AsBsonArray.Select(p => p.AsString).ToArray();
                        post.Date = item.GetElement("post_date").Value.ToString();
                        postInfo.Add(post);
                        count++;
                    }
                }
            }
            var json = new JavaScriptSerializer().Serialize(postInfo);
            return json;
        }