Exemplo n.º 1
0
        static void Main(string[] args)
        {
            SemanticDatabase sd = new SemanticDatabase();
            sd.Open("newdb");
            List<string> collections = sd.GetCollections();

            IMongoClient client = new MongoClient();
            IMongoDatabase db = client.GetDatabase("newdb");
            var collection = db.GetCollection<BsonDocument>("PersonPhone");
            var aggregate = collection.Aggregate().Lookup("Person", "PersonID", "ID", "Person").Lookup("Phone", "PhoneID", "ID", "Phone");

            // Useful link that helped me figure this out: https://groups.google.com/forum/#!topic/mongodb-user/Otg17LUE_7M
            var pipes = new[]
            {
                @"{$lookup:
                    {
                        from: 'Person',
                        localField: 'PersonID',
                        foreignField: 'ID',
                        as: 'PersonName'
                    }}",
                @"{$lookup:
                {
                    from: 'Phone',
                    localField: 'PhoneID',
                    foreignField: 'ID',
                    as: 'PersonPhone'
                }}",
                @"{ $match: {PersonID: 2} }",
                @"{$project: {'PersonName.LastName':1, 'PersonName.FirstName':1, 'PersonPhone.Number': 1, _id:0}}"
            };

            var pipeline = pipes.Select(s => BsonDocument.Parse(s)).ToList();

            // Option 1:
            // var test = collection.Aggregate<BsonDocument>(pipeline);

            // Option 2:
            var test = collection.Aggregate();
            foreach (string pipe in pipes)
            {
                test = test.AppendStage<BsonDocument>(pipe);
            }

            // var filter = new BsonDocument();
            //var result = collection.Find(filter).ToList();
            // var result = aggregate.ToList();

            var result = test.ToList();

            foreach (var doc in result)
            {
                var elements = doc.Elements;

                foreach (var el in elements)
                {
                    Console.WriteLine(el.Name + " = " + el.Value);
                }
            }
        }
 protected void InstantiateMonthLookup(SemanticDatabase sd, Schema schema)
 {
     sd.Insert(schema, BsonDocument.Parse("{month: 1, monthName: 'January', monthAbbr: 'Jan'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 2, monthName: 'February', monthAbbr: 'Feb'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 3, monthName: 'March', monthAbbr: 'Mar'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 4, monthName: 'April', monthAbbr: 'Apr'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 5, monthName: 'May', monthAbbr: 'May'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 6, monthName: 'June', monthAbbr: 'Jun'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 7, monthName: 'July', monthAbbr: 'Jul'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 8, monthName: 'August', monthAbbr: 'Aug'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 9, monthName: 'September', monthAbbr: 'Sep'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 10, monthName: 'October', monthAbbr: 'Oct'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 11, monthName: 'November', monthAbbr: 'Nov'}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 12, monthName: 'December', monthAbbr: 'Dec'}"));
 }
Exemplo n.º 3
0
 public Model()
 {
     Db = new SemanticDatabase();
 }
Exemplo n.º 4
0
 protected void InstantiateTestPersonRecords(SemanticDatabase sd, Schema schema)
 {
     sd.Insert(schema, BsonDocument.Parse("{firstName: 'Marc', lastName: 'Clifton'}"));
     sd.Insert(schema, BsonDocument.Parse("{firstName: 'April', lastName: 'Jones'}"));
 }
Exemplo n.º 5
0
 protected void InstantiateTestDateRecords(SemanticDatabase sd, Schema schema)
 {
     sd.Insert(schema, BsonDocument.Parse("{month: 8, day: 19, year: 1962}"));
     sd.Insert(schema, BsonDocument.Parse("{month: 4, day: 1, year: 2016}"));
 }
Exemplo n.º 6
0
        public static SemanticDatabase CreateCleanDatabase()
        {
            SemanticDatabase sd = new SemanticDatabase();
            sd.DropDatabase("testdb");
            sd.Open("testdb");

            return sd;
        }