Trying to use the Query Builder with the C# Driver

1,020 views
Skip to first unread message

False McFakeman

unread,
May 3, 2016, 10:17:14 PM5/3/16
to mongodb-user
Okay, so I'm trying to compile a program on Ubuntu using Mono, and this is the command I'm using:

gmcs main.cs mongo.cs -r:MongoDB.Bson.dll -r:MongoDB.Driver.dll -r:MongoDB.Driver.Core.dll

I'm just trying to query with the QueryBuilder, but it's telling me things like 'The name `Query' does not exist in the current context'... Unless I try to include MongoDB.Driver.Builders, then it gives me this error instead:

mongo.cs(3,22): error CS0305: Using the generic type `MongoDB.Driver.Builders<TDocument>' requires `1' type argument(s)

But I'm just trying to include a namespace... Notice how it's throwing the error on line 3...

I'm trying to do something like this:

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

// etc.


var collection = _database.GetCollection<BsonDocument>("users");

var query = Query.And(
  Query.EQ("username", username), 
  Query.EQ("password", password) 
);
var fields = Fields.Include("username");
var user = collection.Find(query).SetFields(fields).SetLimit(1).FirstOrDefault();

Type currentType = user.GetType();
return currentType.ToString();

-----

Thanks for any guideance, even if just pointing me to another way to do this that won't error me out.

Craig Wilson

unread,
May 4, 2016, 6:27:49 AM5/4/16
to mongodb-user
The Query class is part of the legacy API. It is in the nuget package mongo-csharp-driver.  If you are going to use the new API, the replacement is Builders<T>.Filter.

Craig
Reply all
Reply to author
Forward
0 new messages