_id in subfield in MongoDB Collection - c#

I'm using C# to supply a MongoDB Collection. The script must guarantee the uniqueness of each Bson document I ingest. To do this I have a field _id . However the _id field is included in another field. And as much as I saw documents are not unique. Documents look like this :
{"metadata" : { {"_id" , "id123"}, {"FileName","aFile42"} ... } ... }
How could I assure that I would get an exception if another Bson come by with the same _id without changing my document structure or without creating another collection as reference collection.

Related

Linq to MongoDB Filter without strong typing

My question is similar to the following except that given answer there doesn't work for me:
Linq to MongoDB Filter
My documents have a lot of elements. I tried to create a class with only some of the elements defined (as below) but that's not working.
public class Test
{
public ObjectId _id;
public string ticker;
}
I get an error that there are other elements that do not match any field in the class.
Is there any way to avoid strong typing? Not all documents in my collection have the same set of elements, so the union of all elements is too much.
Thanks

c# mongoDb 2.0 Not Exist in Dictionary

I want to update a collection which only contains some Id and a dictionary of objectId to objectId.
public class ME_BlaBla
{
[BsonId]
public ObjectId MyId;
public Dictionary<ObjectId, ObjectId> IdsToOtherIds;
}
Im sorry if my names aren't informative, I can't share real code =.
Now, I have this query:
var filter = Builders<ME_BlaBla>.Filter.And(
Builders<ME_BlaBla>.Filter.Eq(t => t.MyId, id),
Builders<ME_BlaBla>.Filter.Not(Builders<ME_BlaBla>.Filter.Exists(t => t.IdsToOtherIds.Values, valueId)),
Builders<ME_BlaBla>.Filter.Not(Builders<ME_BlaBla>.Filter.Exists(t => t.IdsToOtherIds.Keys, keyId)));
So, Im trying to filter by the MyId field but when I want to insert data to there I don't want duplication of any kind, Not in the Keys nor in the Values
The whole idea is that the updating must be atomic and check that neither of the provided ids are contained in the dictionary.
I'm still trying to understand how to use the Existsfilter here, so it might be the answer.
TIA.
EDIT
I changed the code to something like that: (still not sure its working well..cannot test it atm)
Builders<ME_BlaBla>.Filter.Not(Builders<ME_BlaBla>.Filter.ElemMatch(t => t.IdsToOtherIds, a => a.Key == keyId)),
Builders<ME_BlaBla>.Filter.Not(Builders<ME_BlaBla>.Filter.ElemMatch(t => t.IdsToOtherIds, a => a.Value == valueId)));
Builders<ME_BlaBla>.Filter.Not(Builders<ME_BlaBla>.Filter.Exists(t => t.IdsToOtherIds.Values, valueId))
This part of code won't check if valueId value exists in the Values property (which is a list of elements of type ObjectId) of the field Dictionary (that's what you meant, I guess). Exists checks if the document contains certain field; you could check if document of your collection have a field "Dictionary" or "Surname".
If you need a unique value in your application, could you possibly create a singleton class somewhere which will generate next (and thus unique) value of certain sequence?
Your Dictionary probably is serialized as array of documents, so if you need to check whether document already exist in the collection, you need to use AnyIn (or some other) instead of Exists.
You can look nested list existance with your criteria like that,
https://docs.mongodb.org/manual/core/read-operations-introduction/
in your state you can use your dictionaries as 2 union criteria like that,
https://docs.mongodb.org/manual/reference/operator/query/elemMatch/
cheers!

cannot deserialize with the "Id" attribute

I'm trying to deserialize a collection to class.
It seems that in case one of the fields named Id, I will get the error:
base {"An error occurred while deserializing the Address property of
class Person.LicenseEntity: Element 'Id' does not match any field or
property of class Person.Address"} System.FormatException
{System.IO.FileFormatException}
However, changing the field name (e.g. to Idd) in both the class and the collection resolve the problem.
Is it possible that I'm not allowed to use the Id field?
I'm pretty sure this is because the Mongo C# driver deserializes the generated _id field from the document to the property named Id in your class. This means that your Id field has nowhere to go, and explains why changing the name of the Id to Idd allowed it to work.
As you are using a class called Address, I would perhaps name your field AddressId
Have a read of the Mongo C# Driver Docs I'm sure they will be a great help.

Data serialization with and without specific field

I have a simple set of 20+ classes. They are all serializable to allow use of these objects within a web service. (DataContract/DataMember) Each of them has an ID and a variable number of other properties, depending on the class.
And I have a database which will store just an ID, a Name that identifies the class and an XML string. And this XML is also the same data in serialized form, but without one property: the ID field should not be stored, since it's redundant.
But the ID must still be sent to the client of the web service, making things a bit complex. And although I could just create a copy of each class, where one has the ID as DataMember and the other doesn't, I'm just looking for a much cleaner solution to solve this. One where I would not need to store the ID field as part of the XML within the database.
So, question: what is the simplest solution to make sure the ID becomes part of the data that's sent to the client, but skipped when storing it as XML? (Without the need of hacking in the XML to remove it.)
And although I could just create a copy of each class, where one has
the ID as DataMember and the other doesn't
What about inheritance?
public class MyEntity
{
// some props
}
public class MyEntityWithId : MyEntity
{
public int Id { get; set; }
// some props
}

Mongo C# ignore property

I'm using v0.9 of the official MongoDB driver and i'm trying to read in a collection. I have a field in the database that I don't want to read into my object but I get the following error.
"Unexpected element: Network"
The collection looks like this in the database
Merchants
- _id
- Name
- Description
- Url
- Network
When I read it into C# I want to create an object called Merchant that has all of the same properties, except "Network". How do I do this?
There's an "IgnoreExtraElements" option on the BSON serializer which you can enable to prevent that error.
Either set it as an attribute on your Merchant class:
[BsonIgnoreExtraElements]
public Merchant {
// fields and properties
}
or in code if you're using class maps:
BsonClassMap.RegisterClassMap<Merchant>(cm => {
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});

Categories