# MongoDB Xojo Driver (maybe)

**URL:** <https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647>\
**Category:** Databases\
**Created:** [October 15, 2013, 12:50am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647 "2013-10-15T00:50:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [October 15, 2013, 12:50am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/1 "2013-10-15T00:50:09Z")

</div>

All,

I am trying to gather support to help MongoDB (inc) write a driver for Xojo. If you can sign on to the jira case, it can help drive the development of the driver.

[mongodb driver](https://jira.mongodb.org/browse/CDRIVER-236)

thanks!  
sb

---

<div class="post-metadata">

**Author:** ![Dirk\_Cleenwerck](https://forum.xojo.com/user_avatar/forum.xojo.com/dirk_cleenwerck/32/533_2.png) [@Dirk\_Cleenwerck](https://forum.xojo.com/u/Dirk_Cleenwerck)\
**Post date:** [October 15, 2013, 12:30pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/2 "2013-10-15T12:30:38Z")

</div>

Done

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 15, 2013, 12:58pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/3 "2013-10-15T12:58:34Z")

</div>

Added my vote.

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [October 16, 2013, 12:41am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/4 "2013-10-16T00:41:36Z")

</div>

thanks for signing on.

---

<div class="post-metadata">

**Author:** ![Julian\_Mussi](https://forum.xojo.com/user_avatar/forum.xojo.com/julian_mussi/32/445_2.png) [@Julian\_Mussi](https://forum.xojo.com/u/Julian_Mussi)\
**Post date:** [October 16, 2013, 12:45am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/5 "2013-10-16T00:45:07Z")

</div>

added my vote!

---

<div class="post-metadata">

**Author:** ![Peter\_Fargo](https://forum.xojo.com/user_avatar/forum.xojo.com/peter_fargo/32/977_2.png) [@Peter\_Fargo](https://forum.xojo.com/u/Peter_Fargo)\
**Post date:** [October 16, 2013, 11:45am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/6 "2013-10-16T11:45:09Z")

</div>

+1

---

<div class="post-metadata">

**Author:** ![Val\_Raemaekers](https://forum.xojo.com/user_avatar/forum.xojo.com/val_raemaekers/32/320_2.png) [@Val\_Raemaekers](https://forum.xojo.com/u/Val_Raemaekers)\
**Post date:** [October 16, 2013, 12:13pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/7 "2013-10-16T12:13:39Z")

</div>

+1

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 16, 2013, 1:14pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/8 "2013-10-16T13:14:34Z")

</div>

Have to admit that I’ve been avoiding NoSQL until now, and have been sticking with well proven relational databases… but MongoDB looks like an interesting data solution. Busy downloading MongoDB to learn more about it.

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 27, 2013, 8:05pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/9 "2013-10-27T20:05:53Z")

</div>

After spending a few weeks with MongoDB I’m completely sold on the idea that MongoDB offers a lot of value to the data world.

So much so that I started writing a Xojo MongoDB driver in 100% pure Xojo (no external libraries required). I designed the driver according to the requirements given at  
[http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-driver-requirements/](http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-driver-requirements/)

There is still lots to do, but with this first release all the basic required objects are in place (e.g. client, database, collection and cursor) with a few basic methods such as “findOne”, “find” and “insert”. This will allow you to query your MongoDB databases and do basic inserts.

I had to write a BSON Serializer from scratch for the driver, so if try out the driver, please keep in mind that the BSON serializer is only partially implemented (e.g. cannot handle regular expression without them being in quoted… yet…).

Also, this first version of the driver doesn’t support replicated and sharded MongoDB environments yet (only a MongoDB hosted on a single machine). If there is enough interest I’d love to take this driver to become a full implementation. Any feedback anyone might have will really help me a lot in achieving such a goal.

The driver can be downloaded from

[http://www.xojo3d.com/mongodriver/XojoMongoDriver\_0.1.0.zip](http://www.xojo3d.com/mongodriver/XojoMongoDriver_0.1.0.zip)

Simply import the “BSONSerializer” and “MongoDriver” modules into your project. I tried to keep the workings of the driver to be an exact match of the existing drivers. So if you’ve used a MongoDB driver before, it should be easy enough to use the Xojo version of it.

Here is some sample code…

```auto
  Dim client As new MongoDriver.MongoClient() ' connect to localport on default port
  Dim db As MongoDriver.MongoDatabase
  Dim coll As MongoDriver.MongoCollection
  Dim cursor As MongoDriver.MongoCursor
  
  ' wait for connection to be established
  ' NOTE: this will eventually be done automatically by the client constructor
  while not client.IsConnected
    App.DoEvents
  wend
  
  db = client.getDB("test") ' get database object
  
  coll = db.getCollection("data") ' get collection object
  
  coll.insert "{mydoc:123}" ' insert a document into collection
  
  ' query and loop through documents
  
  cursor = coll.find("{mydoc:123}")
  while cursor.hasNext
    System.DebugLog cursor.getNextDocument()
  wend
```

If you want to connect to a MongoDB on a different machine, simply instantiate the client object as follow:

```auto
Dim client As new MongoDriver.MongoClient("some_remote_addr", port#)
```

---

<div class="post-metadata">

**Author:** ![Brad\_Hutchings](https://forum.xojo.com/user_avatar/forum.xojo.com/brad_hutchings/32/143_2.png) [@Brad\_Hutchings](https://forum.xojo.com/u/Brad_Hutchings)\
**Post date:** [October 27, 2013, 8:18pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/10 "2013-10-27T20:18:17Z")

</div>

> [@42767:@Alwyn Bester](#):
>
> So much so that I started writing a Xojo MongoDB driver in 100% pure Xojo (no external libraries required).

2,000,000 bonus points awarded.

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [October 28, 2013, 1:54am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/11 "2013-10-28T01:54:46Z")

</div>

and another 2million bonus points… 🙂

---

<div class="post-metadata">

**Author:** ![Brad\_Hutchings](https://forum.xojo.com/user_avatar/forum.xojo.com/brad_hutchings/32/143_2.png) [@Brad\_Hutchings](https://forum.xojo.com/u/Brad_Hutchings)\
**Post date:** [October 28, 2013, 3:31pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/12 "2013-10-28T15:31:52Z")

</div>

By the way, none of my bonus points were awarded for just making this. You get 250 regular points for that. The 2M bonus points are for making it in 100% pure Xojo. This will be incredibly helpful for solution debuggability.

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 28, 2013, 6:33pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/13 "2013-10-28T18:33:22Z")

</div>

I’m glad you like.

Having it in pure Xojo definitely makes things easier. Luckily the MongoDB driver design is very efficient and the driver code shouldn’t increase the final binary size with too much.

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 29, 2013, 8:45pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/14 "2013-10-29T20:45:20Z")

</div>

MongoDriver 0.1.1 now available… (use at own risk)… I recommend that you first test the driver on a test database and collection before using it for production data.

[http://www.xojo3d.com/mongodriver/MongoDriver\_0.1.1.zip](http://www.xojo3d.com/mongodriver/MongoDriver_0.1.1.zip)

Changes:

- Client object now waits for TCP connection to be established in constructor
- Added MongoClient.Constructor(initAddress) method
- Added client.Update(…)
- Added client.Delete(…)
- Renamed cursor.getNextDocument() to cursor.getNext()
- Added collection.update(, , , )
- Added collection.remove(, )

Sample code:

```auto
  Dim client As new MongoDriver.MongoClient() ' connect to localhost on default port
  Dim db As MongoDriver.MongoDatabase
  Dim coll As MongoDriver.MongoCollection
  Dim cursor As MongoDriver.MongoCursor
  
  db = client.getDB("test") ' get database object
  
  coll = db.getCollection("data") ' get collection object
  
  coll.insert "{mydoc:123}" ' insert document into collection
  
  ' query and loop through documents
  
  cursor = coll.find("{}")
  while cursor.hasNext
    System.DebugLog cursor.getNext()
  wend
  
  coll.remove("{a:1}") ' remove document
  
  coll.update"{mydoc:123}", "{$set:{mydoc:234}}" ' update document
```

According to the MongoDB documentation about the [collection.remove(query,justOne)](http://docs.mongodb.org/manual/reference/method/db.collection.remove/#db.collection.remove) method, justOne should be False by default.

This implies that either one of the following instructions will delete ALL the documents in your collection.

```auto
coll.remove()
coll.remove("{}")
```

This seems a bit dangerous to me, and in I’ve programmed the driver so that justOne=True by default. You have to specifically indicate you want to remove all the documents by setting justOne to False.

If you have any questions, requests or suggestions about the driver, please let me know. Keep in mind, the driver still only supports a database on a single machine. Support for shards and replicas will be added at a later stage.

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [October 30, 2013, 8:06pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/15 "2013-10-30T20:06:43Z")

</div>

To make things easier I’ve started a public GitHub repository for the [Xojo MongoDB driver](https://github.com/alwyn1024/mongodb-xojodriver).

---

<div class="post-metadata">

**Author:** ![Mario\_Prieto](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@Mario\_Prieto](https://forum.xojo.com/u/Mario_Prieto)\
**Post date:** [November 3, 2013, 12:19am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/16 "2013-11-03T00:19:24Z")

</div>

Thanks for your GREAT contribution!! I have been using Python to be able to work with MongoDB, but now I can use Xojo!! I host my databases on www.mongolabs. They have a free database package for developers.

Thanks, keep working on the MongoDB driver!!!

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [November 3, 2013, 10:52am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/17 "2013-11-03T10:52:37Z")

</div>

Glad to hear it helped Mario. I’ll definitely keep working at it.

---

<div class="post-metadata">

**Author:** ![scott\_boss](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@scott\_boss](https://forum.xojo.com/u/scott_boss)\
**Post date:** [November 6, 2013, 3:16pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/18 "2013-11-06T15:16:17Z")

</div>

I have tested out the driver and it works great. Granted I have only done a little testing with it but what I tested worked flawlessly.

thanks for tackling the driver!!

---

<div class="post-metadata">

**Author:** ![Alwyn\_Bester](https://forum.xojo.com/user_avatar/forum.xojo.com/alwyn_bester/32/23099_2.png) [@Alwyn\_Bester](https://forum.xojo.com/u/Alwyn_Bester)\
**Post date:** [November 7, 2013, 7:13am UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/19 "2013-11-07T07:13:00Z")

</div>

You’re welcome Scott, and I’m glad to hear it works.

There is a few things I still want to add to the BSON Serializer (before expanding the objects with more methods)… like regular expressions. The ability to query documents using regular expressions is a powerful feature of MongoDB.

Currently, if you want to query your documents with regular expressions you need to write it as…

MyCollection.find({ field: { $regex: “acme.\*corp”, $options: “i” } })

the following is a nicer syntax IMO to do the same query, but I need to add support for this syntax the BSON Serializer…

MyCollection.find({ field: /acme.\*corp/i })

---

<div class="post-metadata">

**Author:** ![Mark\_Lubratt](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@Mark\_Lubratt](https://forum.xojo.com/u/Mark_Lubratt)\
**Post date:** [November 28, 2013, 3:01pm UTC](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647/20 "2013-11-28T15:01:04Z")

</div>

Thank you for all of the work, Alwyn!

One issue I’m coming up with When I save a new document, the “\_id” field is saved as an ObjectID. When I try to save a modified version of the document, the driver finds the “\_id” field, and tries to do an update; but, the \_id is no longer an ObjectID. This causes a duplicate document to be saved. It actually looks like the ObjectID is being stripped out during the find operation.

Do you have any suggestions on how to handle this?

Thanks!

[Next page](https://forum.xojo.com/t/mongodb-xojo-driver-maybe/11647.md?page=2)
