Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-259

Cannot query a MongoDBRef

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Environment:
      Windows

      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'.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            elranu Mariano Vicario
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: