[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 public class UserData() 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; } } public List<SettingsUser> Settings{ get; set; } } var colletion = db.GetCollection<UserSettings>("UsrSettings"); collection.Find(Query.EQ("usr", new MongoDBRef("UsrSettings", usr._id)); Another try: collection.FindOne(Query.EQ("usr.$id", User._id)); |
| 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); 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() , "Settings" : "Settings for John Doe" } , "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: |
| 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. |