1

I got a complex class (named ArticleData). Now, when i create a bsonDocument using the following code, the resulting bsonDocument automatically gets a document id (even though i try to set "_id" myself):

// bson document created from ArticleData object
var doc = articleData.ToBsonDocument();
doc["_id"] = "12345678";
articleCollection.InsertOne(doc);

I am not able to modify the class ArticleData in any way. Otherwise i could add a field '_id' and set this one to my custom id.

Does anyone know how i can set the id of the bsonDocument to a custom id?

The aim of my coding is to save that bsonDocument to my cosmosDb. In oder to be able to find my documents later i would like to query for my custom id (the unique article id).

Update: Real life example:

{
    "ArticleID" : 9993,
    "ArticleRevisionID" : 9993,
    "StructureID" : 10000,
    "StructureGroupID" : 10578,
    "StructureGroupRevisionID" : 10578,
    "ParentIdentifier" : "PS_1294.2",
    "ModelIdentifier" : "PS_1294.2",
    "ModelNrCatalog" : "1294.2",
    "MaterialNumber" : "624341"
}

resulting bson document:

{
    "_id" : ObjectId("5cd28ea84085af2d6c8318d2"),
    "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson",
    "_v" : {
        "_id" : "12345678"
        "ArticleID" : 9993,
        "ArticleRevisionID" : 9993,
        "StructureID" : 10000,
        "StructureGroupID" : 10578,
        "StructureGroupRevisionID" : 10578,
        "ParentIdentifier" : "PS_1294.2",
        "ModelIdentifier" : "PS_1294.2",
        "ModelNrCatalog" : "1294.2",
        "MaterialNumber" : "624341"
    }
Community
  • 1
  • 1
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • _"the resulting bsonDocument automatically gets a document id"_ - does it? – CodeCaster May 07 '19 at 15:30
  • Yes it does, but i want to be able to set it myself (to be able to find it using a query). Is this possible at all? – Thariama May 08 '19 at 07:18
  • I mean I've never seen that. Can you show a [mcve] with ArticleData's definition and initialization, and the resulting BsonObject's string representation? Ultimately you should be able to assign `articleData["_id"]` yourself. – CodeCaster May 08 '19 at 07:20
  • see my updated question – Thariama May 08 '19 at 08:19
  • It would appear your BsonDocument gets wrapped for some reason. Please mention all relevant code involved. You're using the MongoDB driver? – CodeCaster May 08 '19 at 08:47
  • yes, i am using MongoDB.Driver – Thariama May 08 '19 at 09:06
  • ArticleData is a custom class, but i left out many fields because the whole thing is about 400K in characters big. – Thariama May 08 '19 at 09:12
  • CodeCaster: No, i am not able to set articleData["_id"] because i am not able to modify the class ArticleData. – Thariama May 09 '19 at 08:47

1 Answers1

0

Problem was i created bson documents and inserted those into the cosmos db which lead to the observed wrapping. The correct way of doing this is to write my object into the cosmos db directly, but wrapping them inside a custom CosmosDBData object like this before:

[BsonIgnoreExtraElements]
public class CosmosDbData
{
    [BsonId]
    public string Identifier { get; set; }
    public DateTime LastUpdateUtc { get; set; }
    public object Data { get; set; }
}

I created a new CosmosDbData object (myNewCosmosDbData) and inserted my articleData as Data into the object. Then i was able to set identifier as intended id for the cosmosDb data element:

myNewCosmosDbData.Data = articleData; 
myNewCosmosDbData.Identifier = articleData.ArticleID;

Here the code to insert the elements:

articleCollection.InsertOne(articleData);

This way i was able to set the id without modifying the ArticleData object definition.

Result:

{
    "_id" : "115623",
    "_t" : "CosmosDbData",
    "LastUpdateUtc" : {
        "$date" : -62135596800000
    },
    "Data" : {
        "_t" : "ArticleData",
        "ArticleID" : 115623,
        ...
    }
}
Thariama
  • 50,002
  • 13
  • 138
  • 166