Beispiel #1
0
        public void AutoIndexEngine_Test()
        {
            using (var db = new LiteEngine(new MemoryStream()))
            {
                var doc = new BsonDocument
                {
                    ["name"] = "john doe",
                    ["age"]  = 40
                };

                db.Insert("people", doc);

                var result = db.FindOne("people",
                                        Query.And(
                                            Query.EQ("name", "john doe"),
                                            Query.EQ("age", 40)));

                Assert.AreEqual(doc["name"], result["name"]);

                var indexName = db.GetIndexes("people").FirstOrDefault(x => x.Field == "name");
                var indexAge  = db.GetIndexes("people").FirstOrDefault(x => x.Field == "age");

                // indexes are not unique (by default, when using LiteEngine)
                Assert.AreEqual(false, indexName.Unique);
                Assert.AreEqual(false, indexAge.Unique);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var orig = @"C:\Git\LiteDB\LiteDB.Shell\bin\Debug\net40\teste.db";



            using (var db = new LiteEngine(orig))
            {
                var col = db.GetCollectionNames().First();

                var tt = db.FindAll(col).ToArray();

                var c = db.FindOne("DataObjects_1", Query.EQ("_id", "qyeyeW.1oMJK5"));



                Console.WriteLine(c == null);
            }
            //{
            //    // reading all database
            //    foreach (var col in db.GetCollectionNames())
            //    {
            //        Console.WriteLine("Collection: " + col);
            //
            //
            //        foreach (var doc in db.Find(col, Query.All("Token.LastUsedOn")).Take(10))
            //        {
            //            Console.WriteLine(JsonSerializer.Serialize(doc, true));
            //            // ok
            //        }
            //    }
            //}


            Console.WriteLine("End.");
            Console.ReadKey();
        }
Beispiel #3
0
        public void AutoId_No_Duplicate_After_Delete()
        {
            // using strong type
            using (var db = new LiteDatabase(new MemoryStream()))
            {
                var col = db.GetCollection <EntityInt>("col1");

                col.EnsureIndex(x => x.Name);

                col.Insert(new EntityInt {
                    Name = "One"
                });
                col.Insert(new EntityInt {
                    Name = "Two"
                });

                var one = col.FindOne(x => x.Name == "One");
                var two = col.FindOne(x => x.Name == "Two");

                Assert.AreEqual(1, one.Id);
                Assert.AreEqual(2, two.Id);

                // now delete first 2 rows
                col.Delete(one.Id);
                col.Delete(two.Id);

                // and insert new documents
                col.Insert(new EntityInt {
                    Name = "Three"
                });
                col.Insert(new EntityInt {
                    Name = "Four"
                });

                var three = col.FindOne(x => x.Name == "Three");
                var four  = col.FindOne(x => x.Name == "Four");

                Assert.AreEqual(3, three.Id);
                Assert.AreEqual(4, four.Id);
            }

            // using bsondocument/engine
            using (var db = new LiteEngine(new MemoryStream()))
            {
                db.EnsureIndex("col", "Name");

                db.Insert("col", new BsonDocument {
                    ["Name"] = "One"
                }, BsonType.Int32);
                db.Insert("col", new BsonDocument {
                    ["Name"] = "Two"
                }, BsonType.Int32);

                var one = db.FindOne("col", Query.EQ("Name", "One"));
                var two = db.FindOne("col", Query.EQ("Name", "Two"));

                Assert.AreEqual(1, one["_id"].AsInt32);
                Assert.AreEqual(2, two["_id"].AsInt32);

                // now delete first 2 rows
                db.Delete("col", one["_id"].AsInt32);
                db.Delete("col", two["_id"].AsInt32);

                // and insert new documents
                db.Insert("col", new BsonDocument {
                    ["Name"] = "Three"
                }, BsonType.Int32);
                db.Insert("col", new BsonDocument {
                    ["Name"] = "Four"
                }, BsonType.Int32);

                var three = db.FindOne("col", Query.EQ("Name", "Three"));
                var four  = db.FindOne("col", Query.EQ("Name", "Four"));

                Assert.AreEqual(3, three["_id"].AsInt32);
                Assert.AreEqual(4, four["_id"].AsInt32);
            }
        }