[CSHARP-259] Cannot query a MongoDBRef Created: 29/Jun/11  Updated: 02/Apr/15  Resolved: 03/Jul/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.1

Type: Bug Priority: Major - P3
Reporter: Mariano Vicario Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: mongodbref, query,
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows



 Description   

Question: http://stackoverflow.com/questions/6512228/mongodbref-how-to-write-a-query
I'm working with the MongoDB official driver (10Gen). And I cannot query a MonogoDBRef propertie. I have the following classes:

public class UserData()
{
private ObjectId id;
public ObjectId _id
{
get

{ return id; }
set { id = value; }
}
[BsonElement("Mail")]
public string Email { get; set; }
public string Name{ get; set; }
}
public class UserSettings()
{
private ObjectId id;
public ObjectId _id
{
get { return id; }

set

{ id = value; }

}
[BsonElement("usr")]
public MongoDBRef User

{ get; set; }
public List<SettingsUser> Settings{ get; set; }

}
I want to make a query that having the UserData I fetch the UserSettings of that user. I try the following but it does not work:

var colletion = db.GetCollection<UserSettings>("UsrSettings");
collection.Find(Query.EQ("usr", usr._id);
also I try this:

collection.Find(Query.EQ("usr", new MongoDBRef("UsrSettings", usr._id));
But it does not compile because MongoDBRef is not a BsonValue.

Another try:

collection.FindOne(Query.EQ("usr.$id", User._id));
And I get the exception: Unexpected element '$ref'.



 Comments   
Comment by Robert Stam [ 03/Jul/11 ]

Closed but will reopen if issue is not resolved to user's satisfaction.

Comment by Robert Stam [ 03/Jul/11 ]

The first approach doesn't work because you are comparing an ObjectId to a MongoDBRef, which will never match.

The second approach can work but you have to wrap the MongoDBRef like this:

var dbRef = new MongoDBRef("userdata", user1Data._id);
var query1 = Query.EQ("usr", BsonDocumentWrapper.Create(dbRef));

The third approach should have worked. I suspect you got the exception because you have some existing data that does not match your class declaration. Here's what my test data for the user settings collection looks like:

> db.testusersettings.find()
{ "_id" : ObjectId("4e108809e447ad8e8cc69389"), "usr" :

{ "$ref" : "userdata", "$id" : ObjectId("4e108809e447ad8e8cc69388") }

, "Settings" : "Settings for John Doe" }
{ "_id" : ObjectId("4e108809e447ad8e8cc6938b"), "usr" :

{ "$ref" : "userdata", "$id" : ObjectId("4e108809e447ad8e8cc6938a") }

, "Settings" : "Settings for Jane Doe" }
>

Note that I changed the type of your Settings property to string because I don't have the declaration of your SettingsUser class.

Here's the complete test program you can play with:

http://www.pastie.org/2158732

Comment by Robert Stam [ 03/Jul/11 ]

Can you display the matching document using the mongo shell? Just need to confirm that the existing document matches the class declaration.

Generated at Wed Feb 07 21:36:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.