Skip to content

Database Administration for Diaspora pods (mongo shell)

One advantage of running your own Diaspora pod is the fact, that you have access to the MongoDB which is used to store your Seed. This allows you to do some functions which do not yet have a UI (for example canging the E-Mail address of a user) or which is required to work around alpha hickups (deleting duplicate requests). The mongo shell is a client to the mongodb, which can evaluate JavaScript expressions. It has some macros ("use", "show" and "help"), the other commands are typically JavaScript methods. First you assign a Database Object to the variable "db", you can use that with "use". In the case of Diaspora, the database is named "diaspora-ENVIRONMENT". If you start mongo shell, it will start with the database "test", but you can list all existing databases, and pick one:
$ mongo
connecting to: test
> print('hello JavaScript!')
hello JavaScript!
> show dbs
> use diaspora-development
switched to db diaspora-development
> db
> show collections
Note: you can specify non-existing databases and collections, they are initially empty but will be created, so do not misstype. You can use to see the available methods on a collection object. find() is used to search and display documents. If you specify no parameters it will return a cursort with all documents. The first parameter can be a list of filter criterias, and the second parameters a list of properties to return. mongo shell will iterate and print the first ten entries of a cursor automatically:
> db.users.find({},{username:1,email:1})
{ "_id" : ObjectId("4cf1..02"), "username" : "bernd", "email" : "" }
{ "_id" : ObjectId("4cf1..2d"), "username" : "mela", "email" : "" }
{ "_id" : ObjectId("4cf2..3b"), "username" : null, "email" : "" }
Here you can see, that this pod has 3 users (seeds), two are already established, the third is a pending invite (token). To change the e-mail address of a existing user you can assign the document to a variable, change the desired property and update the collection. For this we use the findOne method, which does return the first match instead of a cursor:
> var bernd = db.users.findOne({username:"bernd"})
> print(
> = ""
> db.users.findOne({username:"bernd"}).email
The above will not create a new document, since the variable "bernd" contains the property _id, and therefore it will update the existing entry in the collection. You need to know that db.users contains the actual users of the pod (i.e. the seeds) and db.people contains a copy of the public part of other seeds (if you received a request from them, or if you added them to an aspect).
> db.requests.findOne()
        "_id" : ObjectId("4cf1...a3"),
        "sent" : true,
        "from_id" : ObjectId("4cf1...03"),
        "to_id" : ObjectId("4cee7...a9"),
        "into_id" : ObjectId("4cf1...9d")
> db.people.findOne({_id:ObjectId("4cf1...03")}).diaspora_handle
> db.people.findOne({_id:ObjectId("4cee7...a9")}).diaspora_handle
> db.aspects.findOne({_id: ObjectId("4cf1...9d")}).name
As you can see I sent a request to "Daniel", inviting him to my aspect "Piraten", but he has not yet responded. Hope this helps you to find your way around in the object model of Diaspora. I recommend you read the MongoDB tutorial to better use the mongo shell, and check the JS API documentation for the details.


No Trackbacks


Display comments as Linear | Threaded

Lutoma on :

Hier fehlt der Flattr-Button!

Whiti on :

Super Artikel, hat mir sehr geholfen!

Schulze on :

Grosse Hilfe, genau den Tipp hatte ich gesucht. Thx for sharing!

Add Comment

BBCode format allowed
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.