Files
spark/src/DataStreamSuite/DataStream/Primitives/MongoDB/MongoDBStream.cs
T

38 lines
1.3 KiB
C#

using System;
using MongoDB.Driver;
using MongoDB.Bson;
class Program {
static void Main() {
// Set the connection string and database name
string connectionString = "mongodb://localhost:27017";
string databaseName = "your_database_name";
string collectionName = "your_collection_name";
// Create a MongoClient
MongoClient client = new MongoClient(connectionString);
// Access the database
IMongoDatabase database = client.GetDatabase(databaseName);
// Access the collection
IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(collectionName);
// Fetch all documents from the collection
var documents = collection.Find(Builders<BsonDocument>.Filter.Empty).ToCursor();
// Iterate through the documents and print values
foreach (var document in documents.ToEnumerable()) {
// Assuming your document structure has fields named "Field1", "Field2", etc.
Console.WriteLine("Field1: " + document["Field1"]);
Console.WriteLine("Field2: " + document["Field2"]);
// Add more fields as needed
// Print a separator between documents
Console.WriteLine(new string('-', 20));
}
}
}