2

I am learning how to use MongoDB from vibed. I wrote simple app, that as I am thinking should do find operation. But when I run it I am getting error: Querying uninitialized MongoCollection.. What I am doing wrong?

import vibe.core.log;
import vibe.db.mongo.mongo;
import vibe.d;
import std.stdio;

import std.array;

void main()
{
    MongoCollection m_posts;
    foreach(p;m_posts.find("{}"))
    {
        writeln(p);
    }
}
Suliman
  • 1,469
  • 3
  • 13
  • 19

2 Answers2

2

There is a mongo example in vibe.d repository.

It comes down to this pattern:

void main()
{   
    auto db = connectMongoDB("localhost").getDatabase("test");
    auto coll = db["collection"];
    foreach (i, doc; coll.find("{}"))
        writeln("Item %d: %s", i, doc.toJson().toString());      
}

In your snippet you have attempted to use collection object without actually connecting to the database and retrieving it from there. This is exactly what error is about.

Mihails Strasuns
  • 3,783
  • 1
  • 18
  • 21
0

You just created the MongoCollection object and did not initialized it with anything. That's why the error is about an "Uninitialized Collection". You should connect it to a database and put some data in it. Have a look at http://vibed.org/api/vibe.db.mongo.collection/MongoCollection for examples.

cym13
  • 799
  • 5
  • 10