1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace ConsoleApplication1 {
    public class User
    {
        public ObjectId Id { get; set; }
        public Location Location { get; set; }
        public DateTime LastAround { get; set; }
    }

    public class Location
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }

    public static class Program
    {
        public static void Main(string[] _args)
        {
            try {
                var server = MongoServer.Create("mongodb://localhost/?safe=true");
                var database = server.GetDatabase("test");
                var collection = database.GetCollection<User>("test");
                collection.Drop();

                var userId = ObjectId.GenerateNewId();
                var user = new User
                {
                    Id = userId,
                    Location = new Location { Latitude = 1.0, Longitude = 2.0 },
                    LastAround = new DateTime(2012, 4, 14, 0, 0, 0, DateTimeKind.Utc)
                };
                collection.Insert(user);

                collection.Update(
                    Query.EQ("_id", userId),
                    Update
                        .SetWrapped<Location>("Location", new Location { Latitude = 3.0, Longitude = 4.0 })
                        .Set("LastAround", new DateTime(2012, 4, 15, 0, 0, 0, DateTimeKind.Utc)));

                collection.Update(
                    Query.EQ("_id", userId),
                    Update
                        .Set("Location.Latitude", 5.0)
                        .Set("Location.Longitude", 6.0)
                        .Set("LastAround", new DateTime(2012, 4, 16, 0, 0, 0, DateTimeKind.Utc)));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled exception:");
                Console.WriteLine(ex);
            }

            Console.WriteLine("Press Enter to continue");
            Console.ReadLine();
        }
    }
}