mongo的objectid如果重复插入如何解决

  这边遇到一个从stockquotes插入每天的股票数据到HistoricalQuote的情况,在重复插入这条一样的ObjectId的时候要新建一条objectId。


        public void PopulateLatestStockQuotesToHistoricalQuotesPerDay()
        {
           
                lock (Locker)
                {
                    Task a = new Task(() =>
                    {
                       
                        Logger.Debug("Getting LatestStockQuoteInfo");
                        var tradeDate = DateTime.UtcNow;
                        var lastWriteTime = MarketDataProvider.GetLastWriteTime();
                        if (lastWriteTime.ToShortDateString() != tradeDate.ToShortDateString())
                        {
                            Logger.Debug("The show2003 Dbf doesn't change intraday.");
                            return;
                        }
                        var stockQuotesInfo = _uow.StockQuotesInfo.GetAll().ToList();
                        foreach (var stockQuote in stockQuotesInfo)
                        {
                            var histQuotes = _uow.HistoricalQuotes.GetAll().Where(m => m.StockCode == stockQuote.SecurityCode).ToList();
                           
                            if (histQuotes.Count > 0)
                            {
                                var latestQuote = histQuotes.OrderByDescending(m => m.TradeDate).First();
                                if (latestQuote.TradeDate < tradeDate)
                                {
                                    HistoricalQuote historicalQuote = Mapper.Map<StockQuoteInfo, HistoricalQuote>(stockQuote);
                                    historicalQuote.Id = new MongoDB.Bson.ObjectId();
                                    historicalQuote.TradeDate = tradeDate;
                                    _uow.HistoricalQuotes.Add(historicalQuote);
                                }
                            }
                            else
                            {
                                HistoricalQuote historicalQuote = Mapper.Map<StockQuoteInfo, HistoricalQuote>(stockQuote);
                                //historicalQuote.Id = new MongoDB.Bson.ObjectId();
                                historicalQuote.TradeDate = tradeDate;
                                _uow.HistoricalQuotes.Add(historicalQuote);
                            }
                         
                        }
                        Logger.Debug("PopulateLatestStockQuotesToHistoricalQuotesPerDay Completed");
                    });
                    a.Start();
                    Task.WaitAll(a);
                }
           

        }


你可能感兴趣的:(mongo的objectid如果重复插入如何解决)