Mongodb collection as dynamicMongoDB vs. CassandraHow to query MongoDB with “like”?How do I drop a MongoDB database from the command line?How to list all collections in the mongo shell?MongoDB collection to .Net dictionary best practiceHow to create own dynamic type or dynamic object in C#?elasticsearch v.s. MongoDB for filtering application“Large data” work flows using pandasWhy not inherit from List<T>?Query mongodb collection as dynamic

To what extent should we fear giving offense?

Why might one *not* want to use a capo?

Should I ask for a raise one month before the end of an internship?

SQL Server - How to achieve READCOMMITED and NOLOCK at the same time?

How to handle inventory and story of a player leaving

If I said I had $100 when asked, but I actually had $200, would I be lying by omission?

Stolen MacBook should I worry about my data?

How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?

Why can't I identify major and minor chords?

What does GDPR mean to myself regarding my own data?

Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?

Does the Tribal card type have inherent mechanical implications?

Are spot colors limited and why CMYK mix is not treated same as spot color mix?

Is Nikon D500 a good fit for nature and ambient-lighting portraits and occasional other uses?

Another "Ask One Question" Question

Should I judge the efficacy of Samadhi based on the ethical qualities of the meditator?

What checks exist against overuse of presidential pardons in the USA?

Why didn't Doc believe Marty was from the future?

Why does AM radio react to IR remote?

Pen test results for web application include a file from a forbidden directory that is not even used or referenced

Did anybody find out it was Anakin who blew up the command center?

How do you say "half the time …, the other half …" in German?

Can someone identify this unusual plane at airport?

Defending Castle from Zombies



Mongodb collection as dynamic


MongoDB vs. CassandraHow to query MongoDB with “like”?How do I drop a MongoDB database from the command line?How to list all collections in the mongo shell?MongoDB collection to .Net dictionary best practiceHow to create own dynamic type or dynamic object in C#?elasticsearch v.s. MongoDB for filtering application“Large data” work flows using pandasWhy not inherit from List<T>?Query mongodb collection as dynamic






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have an application that has two similar but different objects and I want to store those objects in the same collection. What is the best way to do this? And how can I query this collection?



Today my collections is represented by:



public IMongoCollection<Post> Posts

get

return _database.GetCollection<Post>("posts");




And I have this class:



public class Post

public string Id get; set;
public string Message get; set;


public class NewTypePost

public string Id get; set;
public string Image get; set;



So, today I just can save and query using Post class. Now I want to store and retrive the both classes, Post and NewTypePost.



I tried to change the class type from Post to dynamic. But when I did this, I could not query the collections.










share|improve this question


























  • What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

    – dcg
    Mar 27 at 21:27












  • Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

    – Guilherme Ferreira
    Mar 27 at 21:31











  • What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

    – dcg
    Mar 27 at 21:40

















1















I have an application that has two similar but different objects and I want to store those objects in the same collection. What is the best way to do this? And how can I query this collection?



Today my collections is represented by:



public IMongoCollection<Post> Posts

get

return _database.GetCollection<Post>("posts");




And I have this class:



public class Post

public string Id get; set;
public string Message get; set;


public class NewTypePost

public string Id get; set;
public string Image get; set;



So, today I just can save and query using Post class. Now I want to store and retrive the both classes, Post and NewTypePost.



I tried to change the class type from Post to dynamic. But when I did this, I could not query the collections.










share|improve this question


























  • What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

    – dcg
    Mar 27 at 21:27












  • Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

    – Guilherme Ferreira
    Mar 27 at 21:31











  • What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

    – dcg
    Mar 27 at 21:40













1












1








1








I have an application that has two similar but different objects and I want to store those objects in the same collection. What is the best way to do this? And how can I query this collection?



Today my collections is represented by:



public IMongoCollection<Post> Posts

get

return _database.GetCollection<Post>("posts");




And I have this class:



public class Post

public string Id get; set;
public string Message get; set;


public class NewTypePost

public string Id get; set;
public string Image get; set;



So, today I just can save and query using Post class. Now I want to store and retrive the both classes, Post and NewTypePost.



I tried to change the class type from Post to dynamic. But when I did this, I could not query the collections.










share|improve this question
















I have an application that has two similar but different objects and I want to store those objects in the same collection. What is the best way to do this? And how can I query this collection?



Today my collections is represented by:



public IMongoCollection<Post> Posts

get

return _database.GetCollection<Post>("posts");




And I have this class:



public class Post

public string Id get; set;
public string Message get; set;


public class NewTypePost

public string Id get; set;
public string Image get; set;



So, today I just can save and query using Post class. Now I want to store and retrive the both classes, Post and NewTypePost.



I tried to change the class type from Post to dynamic. But when I did this, I could not query the collections.







c# mongodb mongodb-.net-driver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 22:08









mickl

20.6k6 gold badges20 silver badges42 bronze badges




20.6k6 gold badges20 silver badges42 bronze badges










asked Mar 27 at 21:25









Guilherme FerreiraGuilherme Ferreira

5737 silver badges15 bronze badges




5737 silver badges15 bronze badges















  • What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

    – dcg
    Mar 27 at 21:27












  • Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

    – Guilherme Ferreira
    Mar 27 at 21:31











  • What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

    – dcg
    Mar 27 at 21:40

















  • What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

    – dcg
    Mar 27 at 21:27












  • Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

    – Guilherme Ferreira
    Mar 27 at 21:31











  • What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

    – dcg
    Mar 27 at 21:40
















What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

– dcg
Mar 27 at 21:27






What's the problem with having two collections? You can always get the collection as _database.GetCollection<BsonDocument>("posts")

– dcg
Mar 27 at 21:27














Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

– Guilherme Ferreira
Mar 27 at 21:31





Both classes are too similar and used for the same porpouse. If I use two collections I wiil need two query on two places to find what I want. Mongo can store different objects. I do this on node.js, I don't know how to do the same on C# driver.

– Guilherme Ferreira
Mar 27 at 21:31













What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

– dcg
Mar 27 at 21:40





What's the way you want to perform your query? At the end you are going to have to ask for Image or Message right?

– dcg
Mar 27 at 21:40












2 Answers
2






active

oldest

votes


















1















Suppose I have the next conn to a test database:



var mongoClient = new MongoClient(new MongoClientSettings

Server = new MongoServerAddress("localhost"),
);
var database = mongoClient.GetDatabase("TestDb");


Then I can do something like:



var col = database.GetCollection<Post>("posts");
var col2 = database.GetCollection<NewTypePost>("posts");


To get two different instances of IMongoCollection but pointing to the same collection in the database. Further I am able to save to each collection in the usual way:



col.InsertOne(new Post Message = "m1" );
col2.InsertOne(new NewTypePost Image = "im1" );


Then, I'm also able to query from those collection base on the specific fields:



var p1= col.Find(Builders<Post>.Filter.Eq(x=>x.Message, "m1")).FirstOrDefault();
var p2 =col2.Find(Builders<NewTypePost>.Filter.Eq(x=>x.Image, "im1")).FirstOrDefault();

Console.WriteLine(p1?.Message); // m1
Console.WriteLine(p2?.Image); // im1


I don't know if that's what you want but it uses the same collection. BTW, change the Id properties to be decorated with [BsonId, BsonRepresentation(BsonType.ObjectId)]. Hope it helps.






share|improve this answer
































    1















    MongoDB .NET driver offers few possibilites in such cases:



    Polymorphism



    You can build a hierarchy of classes and MongoDB driver will be able to determine a type of an object it gets retrieved from the database:



    [BsonKnownTypes(typeof(Post), typeof(NewTypePost))]
    public abstract class PostBase

    [BsonId]
    public string Id get; set;



    public class Post: PostBase

    public string Message get; set;


    public class NewTypePost: PostBase

    public string Image get; set;



    MongoDB driver will create additional field _t in every document which will represent corresponding class.



    Single Class



    You can still have Post class and use BsonIgnoreIfNull attribute to avoid serialization exception. MongoDB .NET driver will set those properties to null if they don't exist in your database.



    public class Post

    [BsonId]
    public string Id get; set;
    [BsonIgnoreIfNull]
    public string Message get; set;
    [BsonIgnoreIfNull]
    public string Image get; set;



    BsonDocument



    You can also drop strongly-typed approach and use BsonDocument class which is dynamic dictionary-like structure that represents your Mongo documents



    var collection = db.GetCollection<BsonDocument>("posts");


    More details here



    dynamic



    Specifying dynamic as generic parameter of ICollection you should get a list of ExpandoObject that will hold all the values you have in your database.



    var collection = db.GetCollection<dynamic>("posts");
    var data = collection.Find(Builders<dynamic>.Filter.Empty).ToList();
    var firstMessage = data[0].Message; // dynamically typed code





    share|improve this answer



























    • I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

      – Guilherme Ferreira
      Mar 27 at 22:17











    • Your ICollection can be still be of type PostBase if you query on shared fields like Id

      – mickl
      Mar 27 at 22:20













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386687%2fmongodb-collection-as-dynamic%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    Suppose I have the next conn to a test database:



    var mongoClient = new MongoClient(new MongoClientSettings

    Server = new MongoServerAddress("localhost"),
    );
    var database = mongoClient.GetDatabase("TestDb");


    Then I can do something like:



    var col = database.GetCollection<Post>("posts");
    var col2 = database.GetCollection<NewTypePost>("posts");


    To get two different instances of IMongoCollection but pointing to the same collection in the database. Further I am able to save to each collection in the usual way:



    col.InsertOne(new Post Message = "m1" );
    col2.InsertOne(new NewTypePost Image = "im1" );


    Then, I'm also able to query from those collection base on the specific fields:



    var p1= col.Find(Builders<Post>.Filter.Eq(x=>x.Message, "m1")).FirstOrDefault();
    var p2 =col2.Find(Builders<NewTypePost>.Filter.Eq(x=>x.Image, "im1")).FirstOrDefault();

    Console.WriteLine(p1?.Message); // m1
    Console.WriteLine(p2?.Image); // im1


    I don't know if that's what you want but it uses the same collection. BTW, change the Id properties to be decorated with [BsonId, BsonRepresentation(BsonType.ObjectId)]. Hope it helps.






    share|improve this answer





























      1















      Suppose I have the next conn to a test database:



      var mongoClient = new MongoClient(new MongoClientSettings

      Server = new MongoServerAddress("localhost"),
      );
      var database = mongoClient.GetDatabase("TestDb");


      Then I can do something like:



      var col = database.GetCollection<Post>("posts");
      var col2 = database.GetCollection<NewTypePost>("posts");


      To get two different instances of IMongoCollection but pointing to the same collection in the database. Further I am able to save to each collection in the usual way:



      col.InsertOne(new Post Message = "m1" );
      col2.InsertOne(new NewTypePost Image = "im1" );


      Then, I'm also able to query from those collection base on the specific fields:



      var p1= col.Find(Builders<Post>.Filter.Eq(x=>x.Message, "m1")).FirstOrDefault();
      var p2 =col2.Find(Builders<NewTypePost>.Filter.Eq(x=>x.Image, "im1")).FirstOrDefault();

      Console.WriteLine(p1?.Message); // m1
      Console.WriteLine(p2?.Image); // im1


      I don't know if that's what you want but it uses the same collection. BTW, change the Id properties to be decorated with [BsonId, BsonRepresentation(BsonType.ObjectId)]. Hope it helps.






      share|improve this answer



























        1














        1










        1









        Suppose I have the next conn to a test database:



        var mongoClient = new MongoClient(new MongoClientSettings

        Server = new MongoServerAddress("localhost"),
        );
        var database = mongoClient.GetDatabase("TestDb");


        Then I can do something like:



        var col = database.GetCollection<Post>("posts");
        var col2 = database.GetCollection<NewTypePost>("posts");


        To get two different instances of IMongoCollection but pointing to the same collection in the database. Further I am able to save to each collection in the usual way:



        col.InsertOne(new Post Message = "m1" );
        col2.InsertOne(new NewTypePost Image = "im1" );


        Then, I'm also able to query from those collection base on the specific fields:



        var p1= col.Find(Builders<Post>.Filter.Eq(x=>x.Message, "m1")).FirstOrDefault();
        var p2 =col2.Find(Builders<NewTypePost>.Filter.Eq(x=>x.Image, "im1")).FirstOrDefault();

        Console.WriteLine(p1?.Message); // m1
        Console.WriteLine(p2?.Image); // im1


        I don't know if that's what you want but it uses the same collection. BTW, change the Id properties to be decorated with [BsonId, BsonRepresentation(BsonType.ObjectId)]. Hope it helps.






        share|improve this answer













        Suppose I have the next conn to a test database:



        var mongoClient = new MongoClient(new MongoClientSettings

        Server = new MongoServerAddress("localhost"),
        );
        var database = mongoClient.GetDatabase("TestDb");


        Then I can do something like:



        var col = database.GetCollection<Post>("posts");
        var col2 = database.GetCollection<NewTypePost>("posts");


        To get two different instances of IMongoCollection but pointing to the same collection in the database. Further I am able to save to each collection in the usual way:



        col.InsertOne(new Post Message = "m1" );
        col2.InsertOne(new NewTypePost Image = "im1" );


        Then, I'm also able to query from those collection base on the specific fields:



        var p1= col.Find(Builders<Post>.Filter.Eq(x=>x.Message, "m1")).FirstOrDefault();
        var p2 =col2.Find(Builders<NewTypePost>.Filter.Eq(x=>x.Image, "im1")).FirstOrDefault();

        Console.WriteLine(p1?.Message); // m1
        Console.WriteLine(p2?.Image); // im1


        I don't know if that's what you want but it uses the same collection. BTW, change the Id properties to be decorated with [BsonId, BsonRepresentation(BsonType.ObjectId)]. Hope it helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 21:50









        dcgdcg

        1,8727 silver badges18 bronze badges




        1,8727 silver badges18 bronze badges


























            1















            MongoDB .NET driver offers few possibilites in such cases:



            Polymorphism



            You can build a hierarchy of classes and MongoDB driver will be able to determine a type of an object it gets retrieved from the database:



            [BsonKnownTypes(typeof(Post), typeof(NewTypePost))]
            public abstract class PostBase

            [BsonId]
            public string Id get; set;



            public class Post: PostBase

            public string Message get; set;


            public class NewTypePost: PostBase

            public string Image get; set;



            MongoDB driver will create additional field _t in every document which will represent corresponding class.



            Single Class



            You can still have Post class and use BsonIgnoreIfNull attribute to avoid serialization exception. MongoDB .NET driver will set those properties to null if they don't exist in your database.



            public class Post

            [BsonId]
            public string Id get; set;
            [BsonIgnoreIfNull]
            public string Message get; set;
            [BsonIgnoreIfNull]
            public string Image get; set;



            BsonDocument



            You can also drop strongly-typed approach and use BsonDocument class which is dynamic dictionary-like structure that represents your Mongo documents



            var collection = db.GetCollection<BsonDocument>("posts");


            More details here



            dynamic



            Specifying dynamic as generic parameter of ICollection you should get a list of ExpandoObject that will hold all the values you have in your database.



            var collection = db.GetCollection<dynamic>("posts");
            var data = collection.Find(Builders<dynamic>.Filter.Empty).ToList();
            var firstMessage = data[0].Message; // dynamically typed code





            share|improve this answer



























            • I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

              – Guilherme Ferreira
              Mar 27 at 22:17











            • Your ICollection can be still be of type PostBase if you query on shared fields like Id

              – mickl
              Mar 27 at 22:20















            1















            MongoDB .NET driver offers few possibilites in such cases:



            Polymorphism



            You can build a hierarchy of classes and MongoDB driver will be able to determine a type of an object it gets retrieved from the database:



            [BsonKnownTypes(typeof(Post), typeof(NewTypePost))]
            public abstract class PostBase

            [BsonId]
            public string Id get; set;



            public class Post: PostBase

            public string Message get; set;


            public class NewTypePost: PostBase

            public string Image get; set;



            MongoDB driver will create additional field _t in every document which will represent corresponding class.



            Single Class



            You can still have Post class and use BsonIgnoreIfNull attribute to avoid serialization exception. MongoDB .NET driver will set those properties to null if they don't exist in your database.



            public class Post

            [BsonId]
            public string Id get; set;
            [BsonIgnoreIfNull]
            public string Message get; set;
            [BsonIgnoreIfNull]
            public string Image get; set;



            BsonDocument



            You can also drop strongly-typed approach and use BsonDocument class which is dynamic dictionary-like structure that represents your Mongo documents



            var collection = db.GetCollection<BsonDocument>("posts");


            More details here



            dynamic



            Specifying dynamic as generic parameter of ICollection you should get a list of ExpandoObject that will hold all the values you have in your database.



            var collection = db.GetCollection<dynamic>("posts");
            var data = collection.Find(Builders<dynamic>.Filter.Empty).ToList();
            var firstMessage = data[0].Message; // dynamically typed code





            share|improve this answer



























            • I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

              – Guilherme Ferreira
              Mar 27 at 22:17











            • Your ICollection can be still be of type PostBase if you query on shared fields like Id

              – mickl
              Mar 27 at 22:20













            1














            1










            1









            MongoDB .NET driver offers few possibilites in such cases:



            Polymorphism



            You can build a hierarchy of classes and MongoDB driver will be able to determine a type of an object it gets retrieved from the database:



            [BsonKnownTypes(typeof(Post), typeof(NewTypePost))]
            public abstract class PostBase

            [BsonId]
            public string Id get; set;



            public class Post: PostBase

            public string Message get; set;


            public class NewTypePost: PostBase

            public string Image get; set;



            MongoDB driver will create additional field _t in every document which will represent corresponding class.



            Single Class



            You can still have Post class and use BsonIgnoreIfNull attribute to avoid serialization exception. MongoDB .NET driver will set those properties to null if they don't exist in your database.



            public class Post

            [BsonId]
            public string Id get; set;
            [BsonIgnoreIfNull]
            public string Message get; set;
            [BsonIgnoreIfNull]
            public string Image get; set;



            BsonDocument



            You can also drop strongly-typed approach and use BsonDocument class which is dynamic dictionary-like structure that represents your Mongo documents



            var collection = db.GetCollection<BsonDocument>("posts");


            More details here



            dynamic



            Specifying dynamic as generic parameter of ICollection you should get a list of ExpandoObject that will hold all the values you have in your database.



            var collection = db.GetCollection<dynamic>("posts");
            var data = collection.Find(Builders<dynamic>.Filter.Empty).ToList();
            var firstMessage = data[0].Message; // dynamically typed code





            share|improve this answer















            MongoDB .NET driver offers few possibilites in such cases:



            Polymorphism



            You can build a hierarchy of classes and MongoDB driver will be able to determine a type of an object it gets retrieved from the database:



            [BsonKnownTypes(typeof(Post), typeof(NewTypePost))]
            public abstract class PostBase

            [BsonId]
            public string Id get; set;



            public class Post: PostBase

            public string Message get; set;


            public class NewTypePost: PostBase

            public string Image get; set;



            MongoDB driver will create additional field _t in every document which will represent corresponding class.



            Single Class



            You can still have Post class and use BsonIgnoreIfNull attribute to avoid serialization exception. MongoDB .NET driver will set those properties to null if they don't exist in your database.



            public class Post

            [BsonId]
            public string Id get; set;
            [BsonIgnoreIfNull]
            public string Message get; set;
            [BsonIgnoreIfNull]
            public string Image get; set;



            BsonDocument



            You can also drop strongly-typed approach and use BsonDocument class which is dynamic dictionary-like structure that represents your Mongo documents



            var collection = db.GetCollection<BsonDocument>("posts");


            More details here



            dynamic



            Specifying dynamic as generic parameter of ICollection you should get a list of ExpandoObject that will hold all the values you have in your database.



            var collection = db.GetCollection<dynamic>("posts");
            var data = collection.Find(Builders<dynamic>.Filter.Empty).ToList();
            var firstMessage = data[0].Message; // dynamically typed code






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 27 at 22:08

























            answered Mar 27 at 22:00









            micklmickl

            20.6k6 gold badges20 silver badges42 bronze badges




            20.6k6 gold badges20 silver badges42 bronze badges















            • I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

              – Guilherme Ferreira
              Mar 27 at 22:17











            • Your ICollection can be still be of type PostBase if you query on shared fields like Id

              – mickl
              Mar 27 at 22:20

















            • I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

              – Guilherme Ferreira
              Mar 27 at 22:17











            • Your ICollection can be still be of type PostBase if you query on shared fields like Id

              – mickl
              Mar 27 at 22:20
















            I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

            – Guilherme Ferreira
            Mar 27 at 22:17





            I'm looking for something like Polymorphism method. But how can I query on this way? Today I have something like this: dbContext.Posts.ReplaceOneAsync(o => o.Id == posts.Id, postToInsert);

            – Guilherme Ferreira
            Mar 27 at 22:17













            Your ICollection can be still be of type PostBase if you query on shared fields like Id

            – mickl
            Mar 27 at 22:20





            Your ICollection can be still be of type PostBase if you query on shared fields like Id

            – mickl
            Mar 27 at 22:20

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386687%2fmongodb-collection-as-dynamic%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript