none
c# to pull data from MongoDB RRS feed

  • Question


  • Hello,
    I connect to the mongodb server via mongo compass by entering the server name and login details
    In below code, in C#, I am trying to get to the collection and iterate through documents

    Question - 
    1- 
    How do I need to enter login details i.e. username and pwd into the connectionstring below?
    2-
    How is it possible to loop through the document in a collection?
    Notice that I get timeout as mentioned below.

    This is what I have but can not get any further as it timesout on var document.

    Thank  you

    using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Bson.Serialization.Attributes;

    private void frmCRDR_Load(object sender, EventArgs e)
            {
                string connectionString = "mongodb://servername.com:12345";

                MongoClient mongoClient = new MongoClient(connectionString);

                var db = mongoClient.GetDatabase("databaseName");

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

    //The line below gives timeout error - TieoutException was unhandled
               var documents = collection.Find(new BsonDocument()).ToList(); 

                var cursor = collection.Find(new BsonDocument()).ToCursor();
                foreach (var document in cursor.ToEnumerable())
                {
                    Console.WriteLine(document);
                }

            }
    Tuesday, April 2, 2019 7:14 AM

Answers

  • Hi

    Thank you for posting here.

    Since this thread is related to MongoDB, you could post in the following forum.

    https://groups.google.com/forum/#!forum/mongodb-user

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by arkiboys Thursday, April 4, 2019 5:05 AM
    Wednesday, April 3, 2019 5:25 AM
    Moderator