1

Hi I am new to Mongo db I have class structure like this for storing smilies and smilies category. I have made a mongodef property in smilies class for referencing smilies category collection but enable to get smilies category name...

public class smilies {
    public ObjectId _id { get; set; }
    public MongoDBRef scat_name{ get; set; }
    public string smil_url { get; set; }
    public string smil_detail { get; set; }
}

public  class smilies_category {
    public ObjectId _id { get; set; }
    public string scat_name { get; set; }
}

and I am using below code for getting the record and records are coming but only from one document

function void getSmilies(){
  var refDocument = new BsonDocument { 
            {"$ref", "smil_scat_id"}, 
            {"$id", "539ef7c2e46b621314956e3b"}
        };

        var query = Query.EQ("smilies_category", refDocument);
        var result = db.GetCollection("smilies").Find(query)

}

Now What I want here is combine resultset like

 "_id" : ObjectId("539f3ec1e46b62120023d364"),
 "scat_name : "Bussines",
 "smil_url" : "www.gmail.com",
 "smil_detail" : "Ok fine",

How can we achieve this? Is I am going right way? Please let me the know the solution

Rakesh
  • 11
  • 2

1 Answers1

0

your class type is supposed to be as

public class smilies {
    public ObjectId _id { get; set; }
    public MongoDBRef scat_name{ get; set; }
    public string smil_url { get; set; }
    public string smil_detail { get; set; }
    public string[] CategoryName {get; set:}
}

And MongoDB document will be

{
_id:"".
smil_url :"",
...
...

CategoryName:[
            "cat1",
            "Cat2",
            ....
          ]
}

And change your query to equivalent of $in operator in C# driver

HaBo
  • 13,999
  • 36
  • 114
  • 206