Skip to content
Jury Soldatenkov edited this page Nov 24, 2013 · 4 revisions

Installation

The simplest way to do this is to use nuget package manager:

Install-Package Nejdb.Unofficial.

Package contains libraries built for .net40 and .net45. Earlier version are not supported.

Usage

  1. First thing you need is to obtain an instance of nejdb.dll. Recommended way is to use static property Library.Instance. It's safe to reuse across your application, but if there's a reason you need a separated instance of Library - use static method Library.Create(). Note: native library is exported to Path.GetTempFileName(). The file would be deleted when Library.Dispose() called.

  2. Create an instance of EJDB database.

var database = Library.Instance.CreateDatabase();

Please note, it does not create or open database from disk, just object that can operate over database.

  1. Open (or create) database file using
//open database in default mode.
database.Open("biblio.db");
//alternatively you can provide more options when open database. 
database.Open(DbName, Database.DefaultOpenMode | OpenMode.TruncateOnOpen);
  1. Create collection to persist some document in.
var collection = dataBase.CreateCollection("Authors", CollectionOptions.None);
  1. Now we have everything to save our first document. Assume we are going to manage a book store. The first thing wee need to persist in db is authors. Let see document structure:
    //Author name, stored separately. 
    public struct Name
    {
        public string Firstname;

        public string Surname;
    }
    //Extremely short book description
    public struct BookHeader
    {
        public BookHeader(string title, int published) : this()
        {
            Title = title;
            Published = published;
        }

        public string Title { get; set; }

        public int Published { get; set; }
    }
    // The main document - author
    public class Author
    {
        public ObjectId Id { get; private set; }

        public Name Name { get; set; }

        public DateTime BirthDay { get; set; }

        public BookHeader[] MainWorks { get; set; }

        public string[] Hobbies { get; set; }
    }

        [Test]
        public void Experiment_goes_here()
        {
            var originalHamingway = new Author
                         {
                             Name = new Name { Firstname = "Ernest", Surname = "Hemingway" },
                             BirthDay = new DateTime(1899, 7, 21),
                             Hobbies = new[] { "Fishing", "Hunting" },
                             MainWorks = new[]
                                         {
                                             new BookHeader("The Torrents of Spring", 1926),
                                             new BookHeader("The Sun Also Rises", 1926),
                                             new BookHeader("A Farewell to Arms", 1929),
                                             new BookHeader("To Have and Have Not", 1937),
                                             new BookHeader("For Whom the Bell Tolls", 1940),
                                             new BookHeader("Across the River and into the Trees", 1950),
                                             new BookHeader("The Old Man and the Sea", 1952)
                                         }
                         };

            using (var database = Library.Instance.CreateDatabase())
            {
                database.Open("Biblio.db");
                using (var collection = database.CreateCollection("Authors", CollectionOptions.None))
                {
                    //Note, while document not saved id property is empty: 000000000000000000000000
                    Console.WriteLine(originalHamingway.Id);

                    var id = collection.Save<Author>(originalHamingway, false);
                    
                    //When document is save Nejdb automatically assigns id value: 5292021a4d57000000000000 
                    // It happens for documents with property named 'Id' of type ObjectId.  
                    Console.WriteLine(originalHamingway.Id);

                    var reloadedHamingway = collection.Load<Author>(id);
                }
            }
        }
Clone this wiki locally