Mongodb rust driver code error


#1

I have some code that im trying to get working where I write something to a mongodb on a button push to a rpi. Im trying to use mongodb rust driver for this but im getting an error with the doc (cannot find macro doc in this scope).

My code is here https://gist.github.com/anonymous/d455b8386dd5d4832784483d4950beb0. I got the driver code from here https://github.com/mongodb-labs/mongo-rust-driver-prototype. The error comes from the let doc = doc! line

Im no longer using the #macro line


#2

The #[macro_use(..)] attribute needs to be above the thing you’re importing macros from. So in this case you’ve written:

extern crate mongodb;

#[macro_use(bson,doc)]

use sysfs_gpio::{Direction, Pin};

The #[macro_use] attribute only works when applied to an extern crate statement… Did you mean to put it above extern crate bson instead?

#[macro_use(bson,doc)]
extern crate bson;
extern crate mongodb;

#3

It doesn’t matter where I put it it gives the same error


#4

This isn’t a direct answer to your question, but we use a wrapper for the C mongo driver over that pure rust implementation. When evaluating drivers I was scared away from that one because how how clearly they advertise that it is unsupported prototype written by interns and by some of the really scary issues they’ve had in their tracker. They go out of their way to make it clear that it is very much a work in progress.

The wrapper is really well done and we’ve had very little issues with it. The mongo c driver is pretty well battle tested at this point and I’d be pretty hesitant to replace that core. I don’t know how far along you are with your project but might be worth checking out the other library if possible.


#5

ive been able to get it to compile now but am having trouble with it actually writing. What i mean is i set the server and db and collection. but then when i try to write using db.insert_one(doc.clone() … it just seems to stay there and do nothing


#6

I was unable to get this wrapper working. For me, cargo refuses to even compile my app. the help is non-existent to get get it up and running or maybe I’m not reading it correctly. Is there a tutorial somewhere?


#7

Hmmm. I’ll be happy to take a look at your issue but I’d hate to derail this thread. Start a new thread and post your error message and as much of your project as you are willing to share, tag me and I’ll be happy to take a look at it.


#8

I’ve abandoned MongoDB in favour of postgresql. Two reasons mainly, the application I’m working is targeted for a more “traditional” audience so it gives them a chance to reuse their SQL skills. It also lets me introduce JSON in a more familiar way and preach the benefits of document based databases.


#9

Hi dmnte! I’m one of the authors of the mongodb crate (sorry for not seeing this earlier; I don’t generally check the forums for questions about the driver, but someone noticed this and pointed it out to me).

Your gist compiles fine for me when I move the line #[macro_use(bson, doc)] directory above the extern crate bson declaration as mentioned earlier. If you’re still having the issue, could you post the exact output from the compiler here along with the version of rustc that you’re using?