IntroductionMongoDB is a scalable, high-performance, open source, document-oriented database. The concepts used by MongoDB creators match very well Entity concepts. That's why a MongoDB driver is included within the standard Entity Library. Even if Entity is statically typed, defining a MongoDB document is as simple as it is with the mongo interactive shell. ExampleHere is an example of a Mongodb program written in Entity: | class Mongo_socket inherits Socket | | { | | public field waiting_requests: Integer_32: initial_value = 0 ; | | public constructor(self): | | public method overloads incoming_data(self, buffer: object reference on Buffer): | | { | | waiting_requests -- ; | | error_stream.print("incoming_data size=%;\n", [[ buffer->get_size() ]]) ; | | var result = new Mongodb_reply ; | | result->use_buffer(buffer) ; | | var document = result->documents.first ; | | while (document != null) do | | { | | error_stream.print("%r;: %json;\n", [[ document, document ]]) ; | | document ++ ; | | } | | if (result->cursor_id != 0) | | { | | waiting_requests ++ ; | | error_stream.print("send get_more\n") ; | | var get_more = new Mongodb_get_more(result->request_id, "mydb.foo", 10, result->cursor_id) ; | | get_more->send(&self) ; | | } | | public method overloads disconnected(self): | | { | | error_stream.print("disconnected\n") ; | | } | | { | | public field mongo: String: default_value = "127.0.0.1:27017" ; | | public method run(self) returns Integer_32: | | { | | var thread: Thread ; | | test_mongodb(&thread) ; | | return with 0 ; | | } | | public method test_mongodb(self, thread: object reference on Thread): | | { | | var socket = new Mongo_socket ; | | thread->add_socket(socket) ; | | socket->connect("tcp://" + mongo) ; | | var query_1 = new Mongodb_query(22, "mydb.foo", 0, 0, 10, Mongodb_document { t: { $exists : true } }) ; | | socket->waiting_requests ++ ; | | query_1->send(socket) ; | | var update_1 = new Mongodb_update(23, "mydb.foo", 1, Mongodb_document { t: "xx" }, Mongodb_document { $addToSet: { x: { $each: "yellow" , "orange", "cyan" } }, xyz: true }) ; | | update_1->send(socket) ; | | var update_2 = new Mongodb_update(24, "mydb.foo", 1, Mongodb_document { t: "zz" }, Mongodb_document { t: "zz", a: 1, c: false }) ; | | update_2->send(socket) ; | | var update_3 = new Mongodb_update(25, "mydb.foo", 3, Mongodb_document { t: "xx" }, Mongodb_document { $inc: { a: 10.123498765 } }) ; | | update_3->send(socket) ; | | var insert_1 = new Mongodb_insert(26, "mydb.foo", 0) ; | | var a = 87654 ; | | insert_1->documents.insert(Mongodb_document { t: "aa", a: a }) ; | | insert_1->documents.insert(Mongodb_document { t: "ab", a: (a 2) }) ; | | insert_1->documents.insert(Mongodb_document { t: "ac", a: 100 }) ; | | insert_1->documents.insert(Mongodb_document { t: "ad", a: 100 }) ; | | insert_1->documents.insert(Mongodb_document { t: "ae", a: 100 }) ; | | insert_1->send(socket) ; | | var delete_1 = new Mongodb_delete(27, "mydb.foo", 0, Mongodb_document { t: "ac" }) ; | | delete_1->send(socket) ; | | var query_2 = new Mongodb_query(28, "mydb.foo", 0, 0, 10, Mongodb_document { t: { $exists : true } }) ; | | socket->waiting_requests ++ ; | | query_2->send(socket) ; | | error_stream.print("loop\n") ; | | while (socket->waiting_requests > 0) do | This a typical MongoDB program. We first do a query, then three updates and one insert and finally an other query.
|