Insert Arrays into documents from the client app

Hi guys, how to insert a document with some IList variables by the client from the app? The IList field don’t have any setters available…

Can you post some code snippets outlining what you’re trying to do? You don’t need to set the IList property - you can just add values to it.

When the new user logged first time to the app, the app will create a Player document with default values including EmbeddedObject called “disciplines”

var newId = ObjectId.Parse(Authorisation.Instance.RealmUser.Id);
            var discipline = new Discipline()
            {
                DisciplineId = "warrior",
                Circle = 1
            };
            List<Discipline> disciplines = new List<Discipline>();
            disciplines.Add(discipline);
            var player = new Player()
            {
                Id = newId,
                OwnerId = Authorisation.Instance.RealmUser.Id,
                Partition = Authorisation.Instance.RealmUser.Id,
                Name = "Faketa",
                Disciplines = disciplines
            };
            _syncedRealm_privateAccess.Write(() =>
            {
                _syncedRealm_privateAccess.Add(player);
            });

And the Player class:

public class Player : RealmObject
{
[PrimaryKey]
[MapTo(“_id”)]
public ObjectId Id { get; set; }

[MapTo("_partition")]
[Required]
public string Partition { get; set; }

[MapTo("owner_id")]
[Required]
public string OwnerId { get; set; }

[MapTo("name")]
[Required]
public string Name { get; set; }

[MapTo("disciplines")]
public IList<Discipline> Disciplines { get; }

}

public class Discipline : EmbeddedObject
{
[MapTo(“discipline_id”)]
public string DisciplineId { get; set; }

[MapTo("circle")]
public int Circle { get; set; }

}

You can use

var player = new Player()
{
    Id = newId,
    OwnerId = Authorisation.Instance.RealmUser.Id,
    Partition = Authorisation.Instance.RealmUser.Id,
    Name = "Faketa",
    Disciplines =
    {
        discipline
    }
};

Alternatively, you can also add the disciplines to the already created object:

player.Disciplines.Add(new Discipline());

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.