This code is used with the C# driver to select items from a document of items that have a location field value in the range of location id values, I am just providing it as an example:
var locations = new BsonValue[] { 1, 2, 3, 4 };
var data = collection
.Find(Builders<BsonDocument>.Filter.In("LocationId", locations))
.Project(x => Mapper.Map<BsonDocument, ItemViewModel>(x))
.ToListAsync().Result;
Does BsonValue just serve to initialize an array here? Where do I get more information? How do I convert a regular C# list/array into that bson value?
BsonDocument
provides flexible way to represent JSON/BSON in C#. Creating BsonDocument
is similar to creating JSON objects.
Simple document
new BsonDocument("name", "Joe")
creates JSON { "name" : "Joe" }
More complex document
new BsonDocument
{
{"Name", "Joe"},
{
"Books", new BsonArray(new[]
{
new BsonDocument("Name", "Book1"),
new BsonDocument("Name", "Book2")
})
}
}
creates JSON {"Name":"Joe", "Books" : [ { "Name":"Book1" },{ "Name":"Book2" } ]}
Array
new BsonArray(new [] {1, 2, 3})
creates JSON [1,2,3]
Convert C# class to BsonDocument
var product = new Product{ Name = "Book", Pages = 3}.ToBsonDocument()
creates JSON {"Name":"Book","Pages":3}
Implicit conversion helps initialize variables
BsonValue bsonInt = 1;
BsonValue bsonBool = true;
new BsonValue[] { 1, 2, 3, 4 }