1

Either

HashSet<string> aaa { get; set; }

or

List<string> aaa { get; set; }

or

string[] aaa { get; set; }

also store as:

"aaa": {
     "0": "bbb",
     "1": "ccc" 
},

can't store as

"aaa": ["bbb", "ccc"] 

?

0

1 Answer 1

2

The following code with the latest 10gen drivers produces the document in the format you're looking for:

public class MyDocument
{
    public int id { get; set; }
    public string[] aaa { get; set; }
}

class Program
{
    private static void Main(string[] args)
    {
        var client = new MongoClient("mongodb://localhost");
        var server = client.GetServer();
        var db = server.GetDatabase("temp");
        var coll = db.GetCollection("myDocuments");
        var mydoc = new MyDocument
            {
                id = 1,
                aaa = new[] {"bbb", "ccc"}
            };
        coll.Save(mydoc);
    }
}

Here is the resulting document:

/* 0 */
{
    "_id" : 1,
    "aaa" : [ 
        "bbb", 
        "ccc"
    ]
}

IList and List also work. Do you have any custom BSON serialization annotations? Do you have custom class maps?

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

2 Comments

wow,, get result of your case is also: "_id": NumberInt(1), "aaa": { "0": "bbb", "1": "ccc" }. i use nuget "Install-Package mongocsharpdriver" to new console applaction
sorry. i found the crux. i use an html mongo client rockmongo, it has display problem for array.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.