安装K线 Kdata

Published
  /// <summary>
        ///安装K线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void installKdDayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //mongodb 数据库,分步填入股票信息
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("Stock");
            var collection = database.GetCollection<Stockso.Core.Domain.Stocks.Stock>("Stock");

            DzhDataProvider dzhDataProvider = new DzhDataProvider();

            List<KData> listkddata = dzhDataProvider.GetKDataDay("SH600132", -1);



            #region 填入股票名称


            //股票
            //var query = collection.Find(x=>x.StockCode== x.StockCode||x => (string.IsNullOrEmpty(tag)).Project(Builders<Stock>.Projection.Include(doc => doc.StockCode));
            //var results = await query.ToListAsync();


            var t = await collection.Find(x => x.StockInfo.StockCode == "SH600132").Project(Builders<Stock>.Projection.Include(doc => doc.m_kdDay)).FirstOrDefaultAsync();

            var tt = await collection.Find(x => x.StockInfo.StockCode == "SH600132").Project(Builders<Stock>.Projection.Slice("m_kdDay", 2).Include(doc => doc.m_kdDay)).FirstOrDefaultAsync();


            Stock first = await collection.Find(x => x.StockInfo.StockCode == "SH600132").FirstOrDefaultAsync();



            var filter4 = Builders<Stock>.Filter.Eq(x => x.StockInfo.StockCode, "SH600132");
            var opt4 = new FindOptions<Stock>
            {
                Projection = Builders<Stock>.Projection.Include(x => x.m_kdDay).Slice(x => x.m_kdDay, -1, 1)
            };



            //opt4.Skip = -1;
            //opt4.Limit = 1;
            var f2 = await collection.FindAsync(filter4, opt4);


            //1.获取指定长度的数据
            var filter3 = Builders<Stock>.Filter.Eq(x => x.StockInfo.StockCode, "SH600132");
            var opt = new FindOptions<Stock>
            {
                Projection = Builders<Stock>.Projection.Slice(x => x.m_kdDay, -1, 1)

            };



            var f = await collection.FindAsync(filter3, opt);

            try
            {
                using (var cursor = f)
                {
                    while (await cursor.MoveNextAsync())
                    {
                        var batch = cursor.Current;
                        foreach (var document in batch)
                        {

                            foreach (var arr in document.m_kdDay)
                            {

                            }

                            break;
                        }
                        break;
                    }
                }
            }
            catch
            {

            }



            //填入mongodb 数据库 CStockInfo 集合,每只股票一条记录
            var builder = Builders<Stock>.Filter;
            var filter = builder.Eq(x => x.StockInfo.StockCode, "SH600132");
            var update = Builders<Stock>.Update.Set(x => x.m_kdDay, listkddata);

            //列子:ok直接修改list 对象的值 http://stackoverflow.com/questions/12803563/how-to-update-items-in-an-array-list-with-mongo-c-sharp-driver
            //  var update = Builders<Stock>.Update.Set(x => x.m_kdDay[0].m_fOpen, 10);



            await collection.FindOneAndUpdateAsync(filter, update);


            var filter1 = Builders<Stock>.Filter.Eq(x => x.StockInfo.StockCode, "SH600132");
            var projection = Builders<Stock>.Projection.Include(doc => doc.m_kdDay);
            var result = await collection.Find(filter1).Project(projection).FirstAsync();



            //       db.HashSet("urn:stockdb:" + p.stockcode, "stockname", p.stockname);

            //    redis.HSet("stockcodename", Encoding.UTF8.GetBytes(p.stockcode), Encoding.UTF8.GetBytes(p.stockname));





            #endregion


            richTextBox2.AppendText("填入安装K线完成\n");

        }