Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several Weird behaviors when using TimeSeries #178

Closed
paolobriones opened this issue Jul 28, 2022 · 1 comment
Closed

Several Weird behaviors when using TimeSeries #178

paolobriones opened this issue Jul 28, 2022 · 1 comment
Labels
question further information is requested

Comments

@paolobriones
Copy link
Contributor

I was just trying out the time-series functionality and have come across some issues:

//[Collection("quote")]
public class QuoteEntity : Entity {
	public class QuoteMetadata {
		//[Field("symbolpair")]
		public string SymbolPair { get; set; }
	}

	//[Field("metadata")]
	public QuoteMetadata Metadata { get; set; }

	//[Field("timestamp")]
	public DateTime Timestamp { get; set; }

	//[Field("bid")]
	public decimal Bid { get; set; }

	//[Field("ask")]
	public decimal Ask { get; set; }
}
public class QuoteRepository : RepositoryBase<QuoteEntity> {
	static QuoteRepository() {
		try {
			// ISSUE #1: Calling DB.CreateCollectionAsync() when an existing collection already exists
			//			 will throw an exception, to get around this, need to implement a try{ } finally{}

			// ISSUE #2: When an entity class is marked with a [Collection] attribute, mongodb compass shows
			//			 the "QuoteEntity" collection name, but weird, it also shows "quote" collection.
			//			 Not sure if this has something to do with the [Collection] attribute messing with some parameters 

			// --------

			// ISSUE #3: Using nameof(property) which is marked with a [Field] attribute causes exception when inserting
			//DB.CreateCollectionAsync<DatapointEntity>(o => {
			//	o.TimeSeriesOptions = new(nameof(DatapointEntity.Timestamp),
			//						nameof(DatapointEntity.MetaData),
			//						TimeSeriesGranularity.Minutes);
			//}).Wait();

			// QUICKFIX for #3: Instead of using nameof(), must specify the literal name to make it work
			DB.CreateCollectionAsync<DatapointEntity>(o => {
				o.TimeSeriesOptions = new("date", "metadata", TimeSeriesGranularity.Minutes);
			}).Wait();

			// ISSUE #4: The collection is created BUT when you try to insert an entity marked with [Field] attributes
			//		 No insert occurs and multiple exceptions happen in VS2022
		}
		finally {

		}

		DB.DatabaseFor<QuoteEntity>("DBNAME");

		DB.Index<QuoteEntity>()
		  .Key(i => i.Metadata.SymbolPair, KeyType.Ascending)
		  .CreateAsync();

		DB.Index<QuoteEntity>()
		  .Key(i => i.Timestamp, KeyType.Descending)
		  .Key(i => i.Metadata.SymbolPair, KeyType.Ascending)
		  .CreateAsync();
	}
}

I am running this on VS2022 .NET6 (fully patched), MongoDB v5.0.9 (via Docker), MongoDB.Entities v20.26.5

@paolobriones paolobriones changed the title Several Weird behavior when using TimeSeries Several Weird behaviors when using TimeSeries Jul 28, 2022
@dj-nitehawk
Copy link
Owner

  1. yes that's the expected and documented behavior. you can create timeseries collections just once using a migration.
  2. i can't reproduce it. try below code in a fresh console app. works fine.
  3. yes this is not possible because there's no way for TimeSeriesOptions can be made aware of custom field names. you can use reflection and grab the field names yourself and pass it in to the time series options.
  4. can't reproduce it. try below code. works fine.
await DB.CreateCollectionAsync<QuoteEntity>(o =>
    o.TimeSeriesOptions = new("timestamp", "metadata", TimeSeriesGranularity.Minutes));

await DB.InsertAsync(new QuoteEntity
{
    Metadata = new() { SymbolPair = "xy" },
    Timestamp = DateTime.UtcNow,
    Ask = 10,
    Bid = 20
});

[Collection("quote")]
public class QuoteEntity : Entity
{
    public class QuoteMetadata
    {
        [Field("symbolpair")]
        public string SymbolPair { get; set; }
    }

    [Field("metadata")]
    public QuoteMetadata Metadata { get; set; }

    [Field("timestamp")]
    public DateTime Timestamp { get; set; }

    [Field("bid")]
    public decimal Bid { get; set; }

    [Field("ask")]
    public decimal Ask { get; set; }
}

@dj-nitehawk dj-nitehawk added question further information is requested and removed pending investigation labels Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants