Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried getting data from single collection...but i want to get data from below multiple collections...
{ "_id" : 1, "item" : "abc", "price" : 12, "quantity" : 2 }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1 }
{ "_id" : 3  }


{ "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120 }
{ "_id" : 2, "sku" : "def", description: "product 2", "instock" : 80 }
{ "_id" : 3, "sku" : "ijk", description: "product 3", "instock" : 60 }
{ "_id" : 4, "sku" : "jkl", description: "product 4", "instock" : 70 }


What I have tried:

<pre>
        public IEnumerable<Product> GetProducts()
        {
            return _db.GetCollection<Product>("Products").FindAll();
        }


MODEL CLASS:
public class Product
   {
       public ObjectId Id { get; set; }
       [BsonElement("ProductId")]
       public int ProductId { get; set; }
       [BsonElement("ProductName")]
       public string ProductName { get; set; }
       [BsonElement("Price")]
       public int Price { get; set; }
       [BsonElement("Category")]
       public string Category { get; set; }
   }
Posted
Updated 6-Dec-17 23:50pm

1 solution

Hi @sreeenivasa
MongoDB does not support joining, so you only fetch one collection at a time.
if you want to join multiple collection you need to join them with LINQ query syntax, like

var query = _db.GetCollection<Product>("Products").AsQueryable().Join(_db.GetCollection<Item>("Item"),product => product._id,item =>item._id,(product, item) => product; 


hope this will help..
 
Share this answer
 
v3
Comments
sreeenivasa reddy 7-Dec-17 6:19am    
Hi jayantaChatterjee thanks for replying

I have tried the above query but its not working....join method is not there for
a collections object...
JayantaChatterjee 7-Dec-17 6:35am    
please see, I update my solution.
sreeenivasa reddy 7-Dec-17 6:54am    
item =>item._id,(product, item) => product; for item calss not getting any properties.. can't able join based on condition
JayantaChatterjee 7-Dec-17 7:21am    
Sorry,
please change Queryable() to AsQueryable()..
see my updated solution.
sreeenivasa reddy 7-Dec-17 7:27am    
I did sir..but not came

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900