0

I'm trying to store a string values in MongoDB, here is the C# code that I'm currently using:

var filter = Builders<BsonDocument>.Filter.Eq("_id", "pk-0");
var update = Builders<BsonDocument>.Update.Push("address", "MyStreet 1");
    update  = update.AddToSet("Address 2", "MyStreet 2");
await Collection.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true });

Weirdly, both AddToSet() and Push() methods creates an array1 with a string value inside it instead of just a string, see the screenshot below from my Admin UI that shows the result from my code.

enter image description here

Any idea of how I can get my code to store the string directly without putting it inside an array?

1 Answer 1

1

Push and AddToSeat are array operators. If you need to store a scalar value you need to use Set:

var update = Builders<BsonDocument>.Update.Set("address", "MyStreet 1");

Documentation for .Net driver.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.