Is there a way to save enum into the mongoDB? I want to save something like:
public enum SnapshotType {
EVENT,
MEMORY
}
I assume you mean saving an enum value into a collection.
Basically, you just add it into your entity model, like so:
#Document(collection = "MyEntity ")
public class MyEntity {
public SnapshotType snapshotType;
}
It will store it as a string in mongo, and automagically convert when you read it out.
Just save the result. There are no schemas in mongo.
Related
I have a c# model which represents a collection/table in MongoDB. Assume it looks something like:
public class MyModel
{
public ObjectId Id { get; set; }
public ObjectId ClaimId { get; set; }
pub string SomeStringProperty { get; set; }
//..other props
}
This model has several properties with type ObjectId which is part of MonboDB.Bson Package.
Problem: When I index my List<MyModel> to my ElasticSearch server using NEST, all ObjectId type fields of MyModel look like following structure in ElasticSearch:
Question: How can I query the data in ElasticSearch having ObjectId field in 'where' clause of query?
When I have to filter over string field SomeStringProperty of MyModel, I can do following, which works good:
var result =
_elasticClient.Search<MyModel>(x => x
.Index("mymodels")
.Query(q => q
.Term(p => p.SomeStringProperty.Suffix("keyword"), filterValue)
)
)
But how can I apply same filter on ObjectId field? I tried few things but it wasn't matching, and I think that is because its value has completely been changed in elasticsearch server. Is there a way to query over this field?
I went through several articles on SO, but couldn't find similar or exactly like this issue. And secondly, I'm just beginner with ElasticSearch world. Any help/guideline will be appreciated.
I'm trying to make a SQL Query with LinqToEntities which will be as efficient as possible so I would like to avoid iterating after every record I get in results to get a new corrected collection. I'm trying to create a ready collection to send as a result in API just with this one query.
I'm trying to get a discriminator value though with inherited models.
My models are somewhat like these:
public abstract class DbEntity
{
[NotMapped]
public string DiscriminatorValue
{
get
{
return ObjectContext.GetObjectType(GetType()).Name;
}
}
}
public class Foo : DbEntity {
}
public class Bar: DbEntity {
}
This model will create a table in database which will have a column called Discriminator. To get the discriminator value I used my custom DiscriminatorValue property but it won't work in queries like this:
var events = context.MyTable.Select(t => new {
Discriminator = t.DiscriminatorValue
}).ToList();
This below one will obviously work, but it will be much slower imho:
var events = context.MyTable.ToList().Select(t => new {
Discriminator = t.DiscriminatorValue
}).ToList();
Is it possible to get Discriminator value without having to write my custom SQL query myself (like real sql which I know is possible too).
I'm all new to EF and I'm looking for using my own type.
Can I use this type to map it to field at database.
Normally I should map:
DateTime to Date (Oracle)
I need to map:
MyDateType to Date (Oracle)
Can anybody help me to make it?
Is it possible? Maybe other ORM can make it?
U can use another not mapped property of your own type:
public DateTime Date{ get; set; }//this is mapped to db
[NotMapped]
public MyDateType MyDateTimeProperty
{
get { return /*conversion from DateTime to MyDateType */; }
set { Date= /*conversion from MyDateType to DateTime*/; }
}
An application persists Guid field in Mongo and it ends up being stored as BinData:
"_id" : new BinData(3, "WBAc3FDBDU+Zh/cBQFPc3Q==")
The advantage in this case is compactness, the disadvantage shows up when one needs to troubleshoot the application. Guids are passed via URLs, and constantly transforming them to BinData when going to Mongo console is a bit painful.
What are drawbacks of storing Guid as string in addition to increase in size? One advantage is ease of troubleshooting:
"_id" : "3c901cac-5b90-4a09-896c-00e4779a9199"
Here is a prototype of a persistent entity in C#:
class Thing
{
[BsonIgnore]
public Guid Id { get; set; }
[BsonId]
public string DontUseInAppMongoId
{
get { return Id.ToString(); }
set { Id = Guid.Parse(value); }
}
}
In addition to gregor's answer, using Guids will currently prevent the use of the new Aggregation Framework as it is represented as a binary type. Regardless, you can do what you are wanting in an easier way. This will let the mongodb bson library handle doing the conversions for you.
public class MyClass
{
[BsonRepresentation(BsonType.String)]
public Guid Id { get; set;}
}
The drawbacks are that mongodb is optimised to use BSON ObjectID's so it will be slightly less efficient to use strings as ObjectID's. Also if you want to use range based queries on string ObjectIDs then a lexicographic compare will be used which may give different results than you expect. Other than that you can use strings as ObjectIDs.
See Optimizing ObjectIDs
http://www.mongodb.org/display/DOCS/Optimizing+Object+IDs
I am using mongodb with the official c# driver.
I am using Guids as Id field for my objects. I don't want to introduce a dependency on the mongodb bson classes so I am not using ObjectId in my domain layer.
Is it possible to instruct mongodb to insert a creation timestamp into objects that I insert into the datastore?
Example:
public class Foo
{
public Guid Id {get;set;}
public DateTime CreatedOn {get;set;}
}
Using mongodb idGenerators I can get the Guids generated upon insert. I know ObjectId has the timestamp included but as mentioned I wouldn't want my class to look like this
public class Foo
{
public ObjectId Id { get; set; }
public DateTime CreatedOn {get { return Id.CreationTime;}}
}
Is it important that it's the inserted timestamp and not the object's created timestamp? If not then do it in the constructor of the class. Or even better, a base class for your class(es).
public abstract class BaseClass
{
[BsonId]
public Guid Id {get;set;}
public DateTime CreatedOn {get;set;}
protected BaseClass()
{
Guid = new Guid();
CreatedOn = new DateTime.UtcNow;
}
}
public class Foo : BaseClass
{
}
Is this something you can use for it?
You can have an _id which is itself a document :
in json : { _id : {guid : ...., createdOn : ....} , field1 : ..., field2:....}
You just have to modify your idGenerator to have this behavior.
I recommend, however, that you really re-consider to use ObjectId.
If you're trying to make your domain layer pure and free of persistence concerns, then it makes sense to populate the creation date yourself in the domain layer, when deciding to create an entity, instead of relying on the database technology to put in a server timestamp.
This makes "creation date" a logical domain concept rather than the DB's concept of "timestamp when first stored in DB". The two can differ e.g. in cases of migrating data (but keeping the timestamp), deferring execution (e.g. in jobs), etc.
It also creates a healthy separation between "physical timestamp" and "logical timestamp" which you can further exploit during testing/mocking (e.g. you could have a test that says "do X, then change the logical time to 2 days in the future, then assert Y").
Finally, it forces you to think of what the creation date means in your domain layer instead of blindly assuming that it will be correct.
All this being said, if you insist on having it in MongoDB you can have a mapping that creates an ObjectID into some kind of hidden field (e.g. an explicitly-implemented interface) at insert time, and extracts its timestamp into the CreationDate field at read time.